如何将FormArray中的新控件推入另一个FormArray中?

时间:2017-09-11 05:23:00

标签: angular angular-reactive-forms

我正在进行角度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);
  });      
}

我想在每个行业的表单数组中添加新控件。任何人都可以告诉我我该怎么办?

1 个答案:

答案 0 :(得分:1)

let array =(this.couponForm.controls ['applicable_with_industries'])。controls [index] .get('forms');