2 回答
![?](http://img1.sycdn.imooc.com/54584c5e0001491102200220-100-100.jpg)
TA贡献1796条经验 获得超7个赞
是的,您可以编写一个返回 formControl 对象的通用方法。
在您的.ts文件中,编写这样的函数。
getFormControl(controlName: string) {
//return the formControl object of the respective 'controlName'
return this.formName.get(controlName);
}
然后,在您的模板中
<input formControlName="accNum" type="text">
<span *ngIf="getFormControl('accNum').errors?.required">
accNum can't be empty
</span>
<input formControlName="accName" type="text">
<span *ngIf="getFormControl('accName').errors?.required">
accName can't be empty
</span>
![?](http://img1.sycdn.imooc.com/533e4d00000171e602000200-100-100.jpg)
TA贡献1816条经验 获得超4个赞
像这样尝试:
ngOnInit() {
this.formName.valueChanges.subscribe(value => {
Object.keys(this.formName.controls).forEach((key: string) => {
console.log(this.formName.get(key).value);
});
});
}
添加回答
举报