问题
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
回答1:
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}}
回答2:
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
回答3:
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: ''
}));
}
}
来源:https://stackoverflow.com/questions/47754073/getting-issue-in-registering-contorl-with-form-in-angular-reactive-forms