我使用Angular 2中的反应表单模块创建一个包含多个嵌套组的表单。
我的信任'表单中有一系列联系人
<FormArray>this.newTrustForm.controls['contact']
&#39;联系人&#39;中的一个字段group是一组电子邮件&#39;小组,我试着在这里找到它,但唉,不。那么我会在哪里找到它?
<FormArray>this.newTrustForm.controls['contact'].controls['email']
我使用以下内容设置我的表单。
constructor(private _fb: FormBuilder) { }
ngOnInit() {
this.newTrustForm = this._fb.group({
...
contact: this._fb.array([]),
...
});
}
然后我添加&#39;联系&#39;具有以下内容的小组。
initContact() {
return this._fb.group({
...
email: this._fb.array([]),
...
});
}
然后我以相同的方式设置initContactEmail。
答案 0 :(得分:2)
您已指定contact
的索引:
<FormArray>this.newTrustForm.controls['contact'][INDEX]['controls']['email']
或(更具可读性):
this.newTrustForm.get(`contact.${INDEX}.email`) as FormArray;
此外,作为建议,由于contact
和email
为arrays
,您可以将其命名为复数:contacts
和emails
。