Angular2 形式的嵌套组? [英] Nested groups in Angular2 forms?

查看:25
本文介绍了Angular2 形式的嵌套组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular 2 中的响应式表单模块来创建一个包含多个嵌套组的表单.

I'm using the reactive forms module in Angular 2 to create a form with several nested groups.

我的信任"表单中有一系列联系人位于

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.

推荐答案

您已经指定了contact的索引:

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

或者(更易读):

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

另外,作为一个建议,由于 contactemailarrays,您可以将它们命名为复数:contacts和<代码>电子邮件.

Also, as a suggestion, since contact and email are arrays, you could named them in plural: contacts and emails.

这篇关于Angular2 形式的嵌套组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆