0

I have a situation where i have the following Reactive Form structure:

  formGroup: FormGroup = this.fb.group({
    name: ['', Validators.required],
    weight: [1, Validators.required],
    active: true,
    settings: this.fb.array([
     ...
     additionalSettings: FormArray
     ...
     ])
    })
  });

So in the settings formArray there are random pushes happening with formGroups where it has only one FormArray (in this case called "additionalSettings") inside which always has different position in the array. Now finding the key and the array index is fine (it will have fixed object key always). But rendering this nested FormArray on the Html template is hard to resolve. I've tried the following code with no luck, so any help will be appreciated. Thank you.

   <div formArrayName="additionalSettings">
    <div *ngFor="let item of theFormArrayRef.controls; let idx = index;">        
      <div [formGroupName]="idx">
        <input formControlName="name" placeholder="Item name" [value]="item.value.name">
        <input formControlName="placeholder" placeholder="Item name" [value]="item.value.placeholder">
      </div>
    </div>
  </div>

The .ts file:

get theFormArrayRef(): FormArray {
  return this.formGroup.controls['settings'].at(47).controls['additionalSettings'] as FormArray;
  }
4
  • Where exactly are you getting error? Can you please share the error message. Commented Feb 2, 2022 at 11:19
  • I'm getting this error : ERROR Error: Cannot find control with path: 'additionalSettings -> 1 -> name', and all this time whatever i do i get this error. Commented Feb 2, 2022 at 15:29
  • Can you please provide minimal reproducible issue on Stackblitz? It seems the additionalSettings form array has multiple form groups and the 2nd form group doesn't contain name form control, hence the error. Commented Feb 2, 2022 at 16:36
  • stackoverflow.com/questions/63734864/… Commented Feb 5, 2022 at 11:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.