0

我正在使用Angular 2中的反应形式模块来创建一个具有多个嵌套组的表单。Angular2表单中的嵌套组?

我的“信任”的形式具有

<FormArray>this.newTrustForm.controls['contact'] 

其中的“联系人”组中的字段是“邮件”组阵列,我试图在这里找到它触点阵列,但很可惜,没有。我会在哪里找到它?

<FormArray>this.newTrustForm.controls['contact'].controls['email'] 

我使用以下设置我的表单。

constructor(private _fb: FormBuilder) { } 

ngOnInit() { 
    this.newTrustForm = this._fb.group({ 
    ... 
    contact: this._fb.array([]), 
    ... 
    }); 
} 

然后我添加下面的'contact'组。

initContact() { 
    return this._fb.group({ 
    ... 
    email: this._fb.array([]), 
    ... 
    }); 
} 

然后我用同样的方法设置initContactEmail。

+1

你应该告诉我们你是如何创建这些组的。 – Fals

+0

@Fals查看修改。 TIA。 –

+0

我没有看到你迭代每个联系人来创建电子邮件组。如果你在嵌套,你应该这样做。看看官方网站的最后一个例子:https://angular.io/docs/ts/latest/guide/reactive-forms.html#!#form-array – Fals

回答

2

你指定的contact指数:

<FormArray>this.newTrustForm.controls['contact'][INDEX]['controls']['email'] 

或(更易读):

this.newTrustForm.get(`contact.${INDEX}.email`) as FormArray; 

此外,作为一个建议,因为contactemailarrays,你可以在将它们命名为复数:contactsemails