Angular 4: Property “push” and “controls” does not exist on type “AbstractControl”

后端 未结 1 1393
迷失自我
迷失自我 2021-02-05 18:54

I was implemented the code from this link http://plnkr.co/edit/yV94ZjypwBgHAlb0RLK2?p=preview but getting push and controls error.

Here is what i did and don\'t know wha

相关标签:
1条回答
  • 2021-02-05 19:45

    Typescript complains on type checking. You need to cast your control to FormArray. So change

    1)

    this.surveyForm.controls['questions'].push(question);
    

    to

    (<FormArray>this.surveyForm.controls['questions']).push(question);
    

    or

    (this.surveyForm.controls['questions'] as FormArray).push(question);
    

    or

    (this.surveyForm.get('questions') as FormArray).push(question);
    

    2)

    const answers = <FormArray>this.surveyForm.controls.questions.controls[index].controls.answer_ids
    

    to

    const answers = this.surveyForm.get(['questions', index, 'answer_ids']) as FormArray;
    

    or

    const answers = this.surveyForm.get(`questions.${index}.answer_ids`) as FormArray;
    

    Forked Plunker

    0 讨论(0)
提交回复
热议问题