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

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

问题描述

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

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 处获取 products 数组中的元素.如何在不遍历数组的情况下完成此操作?

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:结帐数据和资金从服务器接收.

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

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

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