【问题标题】:how to push new control inside of FormArray which is already inside another FormArray?如何将新控件推送到已经在另一个 FormArray 中的 FormArray 中?
【发布时间】:2017-09-11 18:05:41
【问题描述】:

我正在开发 Angular 4 应用程序,并且正在使用响应式表单。我在创建新表单控件时遇到了一个问题。

打字稿:

couponForm : FormGroup;
"industries": [
            {
                "id": 1,
                "name": "Home Cleaning",
                "is_active": true,
                "forms": [
                {
                    "id": 1,
                    "name": "Form 1",
                }
            },
            {
                "id": 2,
                "name": "Office Cleaning",
                "is_active": true,
                "forms": [
                {
                    "id": 2,
                    "name": "Form 2",
                 }
            }
];

constructor(private formBuilder: FormBuilder)
{
   this.couponForm = this.formBuilder.group({});
}

addIndustriesControl()
{
   this.couponForm.addControl('applicable_with_industries', this.formBuilder.array([]));

   let industriesArray =
 <FormArray>this.couponForm.controls['applicable_with_industries'];

   this.globalSettings.industries.forEach((industry, index) =>{
      let control = this.formBuilder.group({
         industry_id   : [industry.id],
         status        : [],
         forms         : this.formBuilder.array([])
      });

      industriesArray.push(control);
  });      
}

我想在每个行业的表单数组中添加新控件。谁能告诉我该怎么做?

【问题讨论】:

  • 我认为您正在寻找类似 @​​987654321@ 的东西。如果您想要深度嵌套,我在该页面中也有一个 stackblitz 链接
  • 抱歉,我没有从那个链接得到任何东西。
  • 没有得到什么意思吗?使其简短检查stackblitz.com/edit/deep-nested-reactive-form
  • 当我使用这段代码时,让 formArray = this.couponForm.get('applicable_with_industries').controls[index].get('forms');。收到此错误Property 'controls' does not exist on type 'AbstractControl'.
  • 非常感谢您的支持。在您的帮助下,我轻松解决了这个问题。

标签: angular angular-reactive-forms


【解决方案1】:

让数组 = (this.couponForm.controls['applicable_with_industries']).controls[index].get('forms');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2019-10-22
    • 2017-01-06
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多