如何在多个formControl上同时setValidators()?

时间:2017-07-03 14:30:51

标签: angular2-forms angular-validation

是否有其他方法可以最小化代码行。

 this.addRoeForm.controls.supplierName.setValidators(Validators.required);
 this.addRoeForm.controls.fromCurrency.setValidators(Validators.required);
 this.addRoeForm.controls.toCurrency.setValidators(Validators.required);
 this.addRoeForm.controls.roe.setValidators(Validators.required);

1 个答案:

答案 0 :(得分:1)

Object.keys(this.addRoeForm.controls).forEach(key => {
  this.addRoeForm.get(key).setValidators([Validators.required]);
});