ano*_*ous 3 angular2-forms angular angular-reactive-forms
我有这个表单组:
this.form = fb.group({
'teacher': [''],
'schools': fb.array([
fb.group({
'school_name': [''],
'school_description': [''],
})
})
});
Run Code Online (Sandbox Code Playgroud)
问题是:
如何通过函数school_city以FormArray编程方式将新表单控件(命名)添加到特定*组?
要向内部的每个组添加一些控件FormArray,您可以执行以下操作:
someFunc() {
const formArr = this.form.get('schools') as FormArray;
for (const group of formArr.controls) {
group.addControl('school_city', this.fb.control(''));
}
}
Run Code Online (Sandbox Code Playgroud)
编辑#1:
由于OP现在解释他想要添加control到特定组:
您必须传递要添加的组的索引control:
someFunc(idx: number) {
const group = this.form.get(`schools.${idx}`) as FormGroup;
group.addControl('school_city', this.fb.control(''));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2954 次 |
| 最近记录: |