Angular2形式的嵌套组? [英] Nested groups in Angular2 forms?
问题描述
我正在使用Angular 2中的反应表单模块来创建一个包含多个嵌套组的表单。
I'm using the reactive forms module in Angular 2 to create a form with several nested groups.
我的信任表单中包含一系列联系人/ p>
My 'trust' form has an array of contacts at
<FormArray>this.newTrustForm.controls['contact']
联系组中的一个字段是一组电子邮件组,我试着在这里找到它,但是唉,不。然后我会在哪里找到它?
One of the fields in the 'contact' group is an array of 'email' groups and I tried finding it here, but alas, no. Where would I then find it?
<FormArray>this.newTrustForm.controls['contact'].controls['email']
我使用以下内容设置我的表单。
I setup my form with the following.
constructor(private _fb: FormBuilder) { }
ngOnInit() {
this.newTrustForm = this._fb.group({
...
contact: this._fb.array([]),
...
});
}
然后我添加联系人组以及以下内容。
I then add 'contact' groups with the following.
initContact() {
return this._fb.group({
...
email: this._fb.array([]),
...
});
}
然后我以相同的方式设置initContactEmail。
And then I have initContactEmail setup in the same way.
推荐答案
您已指定的索引
:
<FormArray>this.newTrustForm.controls['contact'][INDEX]['controls']['email']
或(更具可读性):
this.newTrustForm.get(`contact.${INDEX}.email`) as FormArray;
另外,作为建议,因为联系
和电子邮件
是数组
,您可以将它们命名为复数形式: contacts
和电子邮件
。
Also, as a suggestion, since contact
and email
are arrays
, you could named them in plural: contacts
and emails
.
这篇关于Angular2形式的嵌套组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!