angularangular-reactive-forms

How to set value of a form control which is inside a form array in Angular reactive form


I've a reactive form:

    this.leDetailsUpdateForm = new FormGroup({
      entityNameArray: new FormArray([
        new FormGroup({
          id: new FormControl(),
          entityName: new FormControl(),
          entityNameStartDate: new FormControl(), // <----- this i want to set
          entityNameEndDate: new FormControl(),
          entityNameStatus: new FormControl(),
          status: new FormControl(),
        }),
      ]),
    })

I'm trying to set it after some calculations as follows :

  setStartDates(e: any) {
    console.log(e);
    this.leDetailsUpdateForm.get('entityNameArray').get('entityNameStartDate').setValue(e)
  }

I'm not getting the required syntax on google. Please help.


Solution

  • you can access FormArray controls by index only so you can try something like

    this.leDetailsUpdateForm.controls['entityNameArray'].controls[0].controls['entityNameStartDate'].setValue(e)