角度2:从FormArray访问数据 [英] Angular 2: Accessing data from FormArray

查看:51
本文介绍了角度2:从FormArray访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用angular2/forms提供的ReactiveForms准备了一个.此表单具有表单数组产品:

I have prepared a from using ReactiveForms provided by angular2/forms. This form has a form array products:

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

productFormGroupArray是FormGroup对象的数组.我使用以下方法获取控件,即FormArray对象:

productFormGroupArray is a array of FormGroup Objects.I fetched the controls i.e. FormArray object using this:

this.checkoutFormGroup.get('products')

我正在尝试在商品数组中获取索引为 i 的元素.如何在不循环遍历数组的情况下完成此操作?

I am trying to get the element in the products array at index i. How can this be done without looping through the array?

我尝试了可用的at(index)方法:

I tried with at(index) method available:

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

但这会产生如下错误:

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

修改2:从服务器收到checkoutData和资金.

Edit 2: checkoutData and fund are received from server.

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

推荐答案

只需将该控件转换为数组

Just cast that control to array

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

它的所有功能都在那里

var item = arrayControl.at(index);

这篇关于角度2:从FormArray访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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