1

I was how wondering how to get controls inside a nested formArray. I initialized this form

 ngOnInit(): void {
     this.surveyForm = this.formBuilder.group({
       'surveyTitle': [null],
       'surveyDescription': new FormControl(null),
       'questionsDetail': this.formBuilder.array([
         this.formBuilder.group({
          'questionType': new FormControl('mcq'),
           'question': new FormControl(null),
           'choices': this.formBuilder.array([])
         })
       ])
     });

but when i try to access the controls of my choices formArray, I get an error. I used this code

get questionsDetailcontrols() {
    return (this.surveyForm.get('questionsDetail') as FormArray).controls;
  }
  get choicesControl()
  {
    return (this.questionsDetailcontrols.get('choices') as FormArray).controls;
  }

I get an error at get('choices') stating that "Property 'get' does not exist on type 'AbstractControl[]'". Can someone show me how to access a controls inside a nester array.

Thanks in Advance

1 Answer 1

2

Try this.

  get questionsDetailcontrols() {
    return this.surveyForm.get('questionsDetail') as FormArray;
  }

  getChoicesControl(index: number)
  {
        return (this.questionsDetailcontrols.at(index).get('choices') as FormArray).controls;
  }
Sign up to request clarification or add additional context in comments.

5 Comments

I still get an error. In fact its the same error but pointing at the at().
I have updated my answer. It will be ok when removed .controls from questionsDetailcontrols
Its working thank you I made some changes though. getchoicesControl(index:number) { return (this.questionsDetailcontrols.at(index).get('choices') as FormArray).controls; }
How come a getter, choicesControl, is accepting a parameter here?
its not a getter but a function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.