Search code examples
angularformsformbuilderformarray

Angular: how to loop through a FormArray which is nested in a FormGroup which is already nested in a different FormGroup?


I use this block of code to create my form:

@Input() fetchedTask: Task;
taskForm: FormGroup;
formThresholds: FormArray;

this.taskForm = this._formBuilder.group({
            taskId: null,
            groupId: this.groupId,
            name: ["", [Validators.required]],
            taskType: this.taskTypeId,
            etc.
            configuration: this._formBuilder.group({
                name: ["", Validators.required],
                path: ["", Validators.required],
                thresholds: this._formBuilder.array([])
            })
        });

I later set values of the form using setValue():

this.taskForm.controls["taskId"].setValue(this.fetchedTask.taskId);

I set the value of my FormArray using:

this.fetchedTask.configuration.thresholds.forEach((x)=>{
              this.addItem(x.value, x.name);
            })

addItem(value: number, name: string): void {
      this.formThresholds = this.taskForm.get('configuration.thresholds') as FormArray;
      this.formThresholds.push(this.createItem(value, name));
    }

createItem(value: number, name: string): FormGroup{
      return this._formBuilder.group({
        value: value,
        name: name
      });
    }

I really don't know how to approach looping through my array values and showing them in my form, with populated values.

I tried this but with no success:

        <div *ngFor="let threshold of taskForm.get('configuration.thresholds') let i = index;">
            <div [formGroupName]="i">
                <input formControlName="name" placeholder="Threshold name">
                <input formControlName="value" placeholder="Threshold value">
            </div>
        </div>

Solution

  • Thanks to Gourav Garg and my fiddling I came up with an answer.

    The problem was that I missed a parent div tag referring to the formGroup in which the formArray belongs - the 'configuration' form group.

    So for this form structure:

    this.taskForm = this._formBuilder.group({
                taskId: null,
                groupId: this.groupId,
                name: ["", [Validators.required]],
                taskType: this.taskTypeId,
                etc.
                configuration: this._formBuilder.group({
                    name: ["", Validators.required],
                    path: ["", Validators.required],
                    thresholds: this._formBuilder.array([])
                })
            });
    
    get thresholds(): FormArray{
          return this.formThresholds = this.taskForm.get('configuration.thresholds') as FormArray;
        }
    
    

    if I want to show values from the threshold on my page I need 4 tags. Example:

    <form [formGroup]="taskForm">
        <div formGroupName="configuration">
            <div formArrayName="thresholds"
                *ngFor="let threshold of this.taskRunProfileForm['controls'].configuration['controls'].thresholds['controls']; let i = index;">
                <div [formGroupName]="i">
                    {{name.value}}
                    {{value.value}}
                </div>
            </div>
        </div>
    </form>