This question already has answers here:
Angular2: Property 'controls' does not exist on type 'AbstractControl'. Error when accessing .control of an object within a formarray thru an index
(4个答案)
去年关闭。
我正在Angular 4中尝试嵌套的反应形式。它工作正常,但是当我尝试构建AOT时会抛出错误
我用谷歌搜索,尝试了几件事,但没有运气。谁能告诉我如何解决此问题?
下面的 typescript 代码
(4个答案)
去年关闭。
我正在Angular 4中尝试嵌套的反应形式。它工作正常,但是当我尝试构建AOT时会抛出错误
我用谷歌搜索,尝试了几件事,但没有运气。谁能告诉我如何解决此问题?
<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>
下面的 typescript 代码
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的评论,首先我改变了
html和 typescript 中的myForm.controls['addresses']
到myForm.get('addresses')
然后根据@yuruzi评论
将myForm.get('addresses').controls
更改为myForm.get('addresses')['controls']
现在工作正常。谢谢@gunter&yuruzi
关于angular - 类型 'controls'上不存在属性 'AbstractControl' Angular 4 [duplicate],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46926182/
10-12 07:19