Angular2: Property 'controls' does not exist on type 'AbstractControl'. Error when accessing .control of an object within a formarray thru an index

前端 未结 4 545
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 04:14

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

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 04:38

    You can use ['controls'] instead of .controls, as below:

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

提交回复
热议问题