2017-09-11 30 views
2

我正在處理角度4應用程序,我正在使用反應形式。我在創建新的表單控件時遇到了一個問題。如何在已經在另一個FormArray中的FormArray內部推新控件?

打字稿:

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); 
    });  
} 

我想在每個行業內形成陣列添加新的控件。任何人都可以告訴我我該怎麼做?

+0

我認爲你正在尋找類似[this](https://rahulrsingh09.github.io/AngularConcepts/reactive)的東西。如果你想深嵌套,我也有一個在該頁面的stackblitz鏈接 –

+0

對不起,但我沒有從該鏈接得到任何東西。 –

+0

沒有得到任何意義?使其簡短檢查https://stackblitz.com/edit/deep-nested-reactive-form –

回答

0

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