Getting problems registering contorl with a form in Angular Reactive forms

I am working on Angular reactive forms. I initially have a formarray in a formgroup. after that I format the form group in formarray to dynamically add form control. I get a problem when binding this control with formControlName. I get this error: cannot find control using path: 'controlArray → 0 → value'

here is my component class code:

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

First I get the data in formsDb, then I looked for it to get the last default form set.

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 any problem in my code template, please help. Thanks

0
2

. , , . , .

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

,

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

,

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

: reviewForm.controls.controlArray.controls[i].controlType formControlName.

. , . https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2

0

Source: https://habr.com/ru/post/1666065/


All Articles