本文介绍了Angular 4类型"AbstractControl"上不存在属性"controls"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Angular 4中尝试嵌套的反应形式.它工作正常,但是当我尝试构建AOT时会抛出错误
I am trying a nested reactive form in Angular 4. It is working fine but when I try to build AOT it's throwing the error
我用Google搜索并尝试了一些尝试,但是没有运气.谁能告诉我如何解决此问题?
I googled and tried few things but no luck. Could anyone tell me how to fix this issue?
<div [formGroup]="myForm">
<div formArrayName="addresses">
<div *ngFor="let address of myForm.get('addresses').controls; let i=index"
class="panel panel-default">
<span *ngIf="myForm.get('addresses').length > 1"
(click)="removeAddress(i)">Remove</span>
<div [formGroupName]="i">
<mat-form-field>
<input matInput formControlName="city" placeholder="city" value="">
</mat-form-field>
</div>
</div>
</div>
<a (click)="addAddress()" style="cursor: default"> Add +</a>
</div>
下面的打字稿代码
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.myForm = this._fb.group({
addresses: this._fb.array([
this.initAddress(),
])
});
}
initAddress() {
return this._fb.group({
city: ['']
});
}
addAddress() {
const control = <FormArray>this.myForm.get('addresses');
control.push(this.initAddress());
}
removeAddress(i: number) {
const control = <FormArray>this.myForm.get('addresses');
control.removeAt(i);
}
推荐答案
基于@GünterZöchbauer的评论,首先我改变了
Based on @Günter Zöchbauer comments , first i changed
myForm.controls['addresses']
到myForm.get('addresses')
然后基于@yuruzi注释
and then based on @yuruzi comment
将myForm.get('addresses').controls
更改为myForm.get('addresses')['controls']
现在可以正常工作了.谢谢@gunter&尤鲁兹
Its working fine now. Thanks @gunter & yuruzi
这篇关于Angular 4类型"AbstractControl"上不存在属性"controls"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!