我正在使用使用 FormArray 的嵌套 FormControl。我的表格是这样的:
let form = new FormGroup({
name: new FormControl(),
email: new FormControl(),
Addresses : new FormArray([
new FormGroup({
building: new FormControl(),
society : new FormControl(),
}),
new FormGroup({
building: new FormControl(),
society : new FormControl(),
})
new FormGroup({
building: new FormControl(),
society : new FormControl(),
})
])
})
addNewAddress() {
let newAddress = new FormGroup({
building: new FormControl(),
society : new FormControl(),
});
this.form.get("addresses").push(newAddress)
}
并在 HTML 模板中
<div *ngFor="let controlGroup of form.get('addresses').controls; let i=index">
<ng-container [formGroup]="controlGroup">
<input type="text" formControlName="building" />
<input type="text" formControlName="society" />
</ng-container>
</div>
**但上述代码返回Build Error
的控件在 abstractControl 上不存在。**
所以我转换form.get('addresses')
成FormControl
使用这个getter Melthod。
get addressArray() : FormArray {
return form.get('addresses') as FormArray
}
//And use it in HTML like this
<div *ngFor="let controlGroup of addressArray.controls; let i=index">
<ng-container [formGroup]="controlGroup"> <== now here is build error
<input type="text" formControlName="building" />
<input type="text" formControlName="society" />
</ng-container>
</div>
在那次转换之后。新的构建错误引发了controlGroup does not contain this methods : addControl, removeCotnrol and ...
因为addressArray.**controls**
返回数组而abstractControl
不是指令需要。FormControl
[formGroup]
FormControl
我怎样才能*ngFor
像这样定义类型:
let (controlGroup: FormControl) of addressArray.controls; let i=index
这可能吗?
请帮我。