0
votes

I am working on Angular Reactive forms. Initially I have an formarray in formgroup. afterwards I push formgroup into formarray to add form control dynamically. I am getting an issue when binding these control using formControlName. I am getting this Error: Cannot find control with path: 'controlArray -> 0 -> value'

here is my component class code:

ngOnInit(){
        this.reviewForm = this.fb.group({            
            'controlArray': new FormArray([])
        });        
        this.showDefaultForm();                     
    }

First I am getting data in formsDb, then I am searching into it to get the last set default form.

showDefaultForm(){
        for(let form of this.formService.formsDb){
            if(form.formName==this.formService.defaultForm.formName){
                this.selectedForm = form;
                this.addForm();
            }
        }     
    }


addForm(){            
        for (let control of this.selectedForm.controlsArr){                              
            (<FormArray>this.reviewForm.get('controlArray')).push(this.fb.group([{
                controlType: control.controlType,
                label: control.label,               
                value: ''
            }])); 
        }        
    } 

Here is my template code:

<form [formGroup]="reviewForm" (ngSubmit)="onSubmit()">
  <div class="example-label">
    <span class='block'>               
    <div formArrayName="controlArray">
      <div *ngFor="let control of reviewForm.get('controlArray').controls; let i = index">
        <div [formGroupName]="i">
          <table>
            <tr>
              <span *ngIf="control.value">
                  <td> 
                    <label>{{control.value.label}}</label> 

              </td>
              <td><span *ngIf="control.value.controlType == 'select'">                
                    <md-select placeholder="{{control.value.label}}" formControlName="value">
                      <md-option *ngFor="let option of control.value.options; let j=index" 
                      [value]="option">{{control.value.options[j]}}</md-option>                  
                    </md-select>
                  </span></td>
              <td> <span *ngIf="control.value.controlType == 'text'">
            <md-form-field class="example-full-width">
              <input mdInput type="text" placeholder="{{control.value.placeholder}}" formControlName="value" >
            </md-form-field>  
        </span></td>                           
              </span>
            </tr>
          </table>
        </div>
      </div>      
    </div>
    </span>
  </div>
</form>

Is there some issue in my template code, Please help. Thanks

3
What "this.selectedForm" variable does have?Pramod Patil
it has form data i.e. form name and form controls. Its class is: export class FormData{ constructor(public formName:String, public controlsArr:Control[]){} }Waleed Shahzaib
what is this.showDefaultForm()?AJT82
I've edited it in question. Please have a look into it.Waleed Shahzaib
Why are you using array here this.fb.group([{?yurzui

3 Answers

0
votes

Change this below condition with dynamic value. Since the information given by you is minimal. Lets try this below and check whether issue is getting resolved or not.

{{control.value[i].controlType}}
0
votes

I think the problem is in this line

<div *ngFor="let control of reviewForm.get('controlArray').controls; let i = index"> 

Please, change it on

<div *ngFor="let control of reviewForm.controls.controlArray.controls; let i = index">

And than you can work with values: reviewForm.controls.controlArray.controls[i].controlType or using formControlName.

Sorry If I understood your problem wrong. I suppose this article will be helpful for you. https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2

0
votes

i think the problem is in the addForm function, when you push the group, you no need create it with brackets as an array, something like that:

   addForm(){
      for (let control of this.selectedForm.controlsArr) {
        (<FormArray>this.reviewForm.get('controlArray')).push(this.fb.group({
          controlType: control.controlType,
          label: control.label,
          value: ''
        }));
      }
    }