我正在研究 Angular Reactive 表单。最初我在formgroup中有一个formarray。之后我将 formgroup 推入 formarray 以动态添加表单控件。使用 formControlName 绑定这些控件时遇到问题。我收到此错误:找不到带有路径的控件:'controlArray -> 0 -> value'
这是我的组件类代码:
ngOnInit(){
this.reviewForm = this.fb.group({
'controlArray': new FormArray([])
});
this.showDefaultForm();
}
首先我在 formsDb 中获取数据,然后我正在搜索它以获取最后设置的默认表单。
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: ''
}]));
}
}
这是我的模板代码:
<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>
我的模板代码有问题吗,请帮忙。谢谢