2016-11-17 46 views
11

我已經準備好了使用angular2/forms提供的ReactiveForms。這種形式的一種形式陣列產品:Angular 2:從FormArray訪問數據

this.checkoutFormGroup = this.fb.group({ 
      selectedNominee: ['', Validators.required], 
      selectedBank: ['', Validators.required], 
      products: productFormGroupArray 
     }); 

productFormGroupArray是FormGroup Objects.I的陣列獲取的控件使用該IE FormArray對象:

this.checkoutFormGroup.get('products') 

我想獲得的產品元素陣列在索引i。如何在不循環數組的情況下做到這一點?

編輯:

我在可用的(索引)方法嘗試:

this.checkoutFormGroup.get('products').at(index) 

但這產生誤差爲:

Property 'at' does not exist on type 'AbstractControl'. 

編輯2: checkoutData和資金從服務器接收。

this.checkoutData.products.forEach(product => { 
        this.fundFormGroupArray.push(this.fb.group({ 
         investmentAmount: [this.fund.minInvestment, Validators.required], 
         selectedSubOption: ['', Validators.required], 
        })) 
      }); 
+0

請分享它告訴你如何定義的代碼'productFormGroupArray' – ranakrunal9

+0

@ ranakrunal9編輯的題 –

回答

13

只投控制到陣列

var arrayControl = this.checkoutFormGroup.get('products') as FormArray; 

和它的所有功能都存在

var item = arrayControl.at(index);