本文介绍了Angular2:属性"controls"在类型"AbstractControl"上不存在.通过索引访问formarray中的对象的.control时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在formarray中推送另一个formbuilder,但是它给了我一个错误,因为我认为初始化代码时数组中没有项目,因此没有控件.错误是
I'm trying to push another formbuilder within a formarray but it gives me an error since I think there are no items in the array when initializing the code, hence there are no controls. The error is Property 'controls' does not exist on type 'AbstractControl' after the
(<FormArray>this.loanTypeForm.controls['frequency']).controls[index]
我正在使用角度2.0.0-beta.17
I'm using angular 2.0.0-beta.17
let settingsForm: FormArray = new FormArray([]);
(<FormArray>this.loanTypeForm.controls['frequency']).push(
this.formBuilder.group({
'name': [value, Validators.required],
'settings': settingsForm,
})
);
(<FormArray>this.loanTypeForm.controls['frequency']).controls[index].controls['settings'].push(
this.formBuilder.group({
'term': [null, Validators.required],
'eir': [null, Validators.required],
})
);
推荐答案
您可以使用['controls']
代替.controls
,如下所示:
You can use ['controls']
instead of .controls
, as below:
(<FormArray>this.loanTypeForm.controls['frequency']).controls[index]['controls']['settings'].push(...)
但是为了简化并提供更高的可读性,建议您将其全部更改为:
But in order to simplify and provide more readability I'd suggest you to change it all to:
const control = this.loanTypeForm.get(`frequency.${index}.settings`) as FormArray;
control.push(...);
这篇关于Angular2:属性"controls"在类型"AbstractControl"上不存在.通过索引访问formarray中的对象的.control时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!