1

I'm trying to display form array inside another formarray but it gives me an error, Cannot read property 'controls' of undefined

here is my code

addInventoryFormGroup() {
    this.addInventoryForm = this.fb.group({
        addInventoryFieldsList: this.fb.array([this.buildAddInventoryFields()]),
    });
}

buildAddInventoryFields(): FormGroup {
    return this.fb.group({
        buildTableFields: this.fb.array([this.buildAddInventoryTableFields()]),
        quantity: '',
        section: '',
        row: '',
    })
}

buildAddInventoryTableFields(): FormGroup {
    return this.fb.group({
        unitCost: '',
        faceValue: '',
    })
}

get addInventoryFieldsList(): FormArray {
    return <FormArray>this.addInventoryForm.get('addInventoryFieldsList');
}

here is my html code for it

<form [formGroup]="addInventoryForm">
     <div class="reactive-form" formArrayName="addInventoryFieldsList" *ngFor="let address of addInventoryFieldsList.controls; let i = index;">
          <div [formGroupName]="i">
               <div formArrayName="buildTableFields" *ngFor="let fields of addInventoryFieldsList.controls.buildTableFields.controls; let x = index;">
                    <span [formGroupName]="x">ABC</span>
               </div>   
          </div>
     </div>
</form>   

Please help me to fix this error

2
  • Can you also post code of addInventoryFieldsList property? Commented May 10, 2018 at 11:39
  • it is just a name Commented May 10, 2018 at 11:40

3 Answers 3

1

Since i don't see you declared addInventoryFieldsList i suspect that is your problem.

You should write addInventoryForm.controls.addInventoryFieldsList to access addInventoryFieldsList FormArray control.

The following should work for you:

<form [formGroup]="addInventoryForm">
    <div class="reactive-form" formArrayName="addInventoryFieldsList" *ngFor="let address of addInventoryForm.controls.addInventoryFieldsList.controls; let i = index;">
        <div [formGroupName]="i">
            <div formArrayName="buildTableFields" *ngFor="let fields of address.controls.buildTableFields.controls; let x = index;">
                <span [formGroupName]="x">ABC</span>
            </div>
        </div>
    </div>
</form>

Also note i use address.controls.buildTableFields.controls to loop over nested controls

Update:

As it turned out you have defined addInventoryFieldsList getter. So here is updated template:

<form [formGroup]="addInventoryForm">
    <div class="reactive-form" formArrayName="addInventoryFieldsList" *ngFor="let address of addInventoryFieldsList.controls; let i = index;">
        <div [formGroupName]="i">
            <div formArrayName="buildTableFields" *ngFor="let fields of address.controls.buildTableFields.controls; let x = index;">
                <span [formGroupName]="x">ABC</span>
            </div>
        </div>
    </div>
</form>

Ng-run Example

Sign up to request clarification or add additional context in comments.

6 Comments

i declared addInventoryFieldsList, i do not have any error if i do not try to display nested forArray
Updated example ng-run.com/edit/WsXrq6fufRFoT1siG9nG It still works for me
yes you are right, its working there but still not on my machine :(
I don't think so. Something is different) Can you recreate example?
html and ts code is 100%, now its display it for 1 sec and then gives same error
|
1

This post might help you... https://alligator.io/angular/reactive-forms-formarray-dynamic-fields/ Be sure to load the form before using it

ngOnInit() {
   this.addInventoryForm = this.fb.group({
    addInventoryFieldsList: this.fb.array([this.buildAddInventoryFields()]),
   });
}... follow with your methods

HTML

   <form [formGroup]="addInventoryForm">
 <div class="reactive-form" formArrayName="addInventoryFieldsList" *ngFor="let address of addInventoryForm.get('addInventoryFieldsList').controls; let i = index;">
      <div [formGroupName]="i">
           <div formArrayName="buildTableFields" *ngFor="let fields of addInventoryForm.get('addInventoryFieldsList').controls; let x = index;">
                <span [formGroupName]="x">ABC</span>
           </div>   
      </div>
 </div>

5 Comments

Add two groups to addInventoryFieldsList and enjoy error) stackblitz.com/edit/angular-8dy7mz?file=src/app/…
@yurzui means ?
@yurzui not sure if your update to my stackblitz was what Arslan meant, the question didn't give much information, but your answer was very rude. Be more polite next time!
Sorry if my answer was rude for you. I just wanted to say that your nested ngFor is not correct. I will be more polite next time
I think that to be rude is to downvote and don't say what's the reason.
0

Please check addInventoryFieldsList which might not be a declared variable in your ts file.

2 Comments

its declared, everything is fine if i do not try to show nested formarray
Can you try with <div formArrayName="buildTableFields" *ngFor="let fields of address.controls.buildTableFields.controls; let x = index;"> <span [formGroupName]="x">ABC</span> </div>

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.