属性 'controls' 在类型 'AbstractControl' 上不存在 Angular 4
Property 'controls' does not exist on type 'AbstractControl' Angular 4
我正在尝试 Angular 4 中的嵌套反应形式。它工作正常但是当我尝试构建 AOT 时它抛出错误
'controls' does not exist on type 'AbstractControl'
我用谷歌搜索并尝试了一些东西,但没有成功。谁能告诉我如何解决这个问题?
<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);
}
要获得 FormArray
的长度,只需使用 length
:
<span *ngIf="myForm.controls['addresses'].length > 1" (click)="removeAddress(i)">Remove</span>
希望对您有所帮助
根据@Günter Zöchbauer 的评论,首先我更改了
myForm.controls['addresses']
到 myForm.get('addresses')
html 和 typescript
然后根据@yuruzi 评论
已将 myForm.get('addresses').controls
更改为 myForm.get('addresses')['controls']
它现在工作正常。谢谢@gunter & yuruzi
将 myForm.get('addresses').controls
更改为 myForm.get('addresses').value
也会解决此问题。
不过您可以轻松修复它。将 "get the controls" 逻辑外包到组件代码(.ts
文件)的方法中:
getControls() {
return (this.recipeForm.get('controlName') as FormArray).controls;
}
在模板中,您可以使用:
*ngFor="let ingredientCtrl of getControls(); let i = index"
由于 TS 的工作方式和 Angular 解析您的模板(它不理解那里的 TS),因此需要进行此调整。
对于验证错误使用...
<span *ngIf="f.YOUR_FORM_KEY.controls.YOUR_FORM_KEY.errors?.YOUR_FORM_VALIDATION">YOUR_FORM_KEY is YOUR_FORM_VALIDATION</span>
例如
<span *ngIf="f.name.controls.name.errors?.required">Name is required</span>
ts 文件
get f(): any {
return this.userForm.controls;
}
作为对@sunny kashyap 解决方案的更新,我会这样写:
getControls() {
return (this.recipeForm.get('controlName') as FormArray).controls;
}
可以使用自定义界面
// Define AbstractFormGroup
export interface AbstractFormGroup extends FormGroup {
controls: {
[key: string]: AbstractFormGroup & AbstractFormGroup[] & AbstractControl & FormGroup & FormArray,
}
}
// Usage example
class ... {
myForm: AbstractFormGroup
...
this.myForm = this.fb.group({...}) as AbstractFormGroup
}
我正在尝试 Angular 4 中的嵌套反应形式。它工作正常但是当我尝试构建 AOT 时它抛出错误
'controls' does not exist on type 'AbstractControl'
我用谷歌搜索并尝试了一些东西,但没有成功。谁能告诉我如何解决这个问题?
<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);
}
要获得 FormArray
的长度,只需使用 length
:
<span *ngIf="myForm.controls['addresses'].length > 1" (click)="removeAddress(i)">Remove</span>
希望对您有所帮助
根据@Günter Zöchbauer 的评论,首先我更改了
myForm.controls['addresses']
到 myForm.get('addresses')
html 和 typescript
然后根据@yuruzi 评论
已将 myForm.get('addresses').controls
更改为 myForm.get('addresses')['controls']
它现在工作正常。谢谢@gunter & yuruzi
将 myForm.get('addresses').controls
更改为 myForm.get('addresses').value
也会解决此问题。
不过您可以轻松修复它。将 "get the controls" 逻辑外包到组件代码(.ts
文件)的方法中:
getControls() {
return (this.recipeForm.get('controlName') as FormArray).controls;
}
在模板中,您可以使用:
*ngFor="let ingredientCtrl of getControls(); let i = index"
由于 TS 的工作方式和 Angular 解析您的模板(它不理解那里的 TS),因此需要进行此调整。
对于验证错误使用...
<span *ngIf="f.YOUR_FORM_KEY.controls.YOUR_FORM_KEY.errors?.YOUR_FORM_VALIDATION">YOUR_FORM_KEY is YOUR_FORM_VALIDATION</span>
例如
<span *ngIf="f.name.controls.name.errors?.required">Name is required</span>
ts 文件
get f(): any {
return this.userForm.controls;
}
作为对@sunny kashyap 解决方案的更新,我会这样写:
getControls() {
return (this.recipeForm.get('controlName') as FormArray).controls;
}
可以使用自定义界面
// Define AbstractFormGroup
export interface AbstractFormGroup extends FormGroup {
controls: {
[key: string]: AbstractFormGroup & AbstractFormGroup[] & AbstractControl & FormGroup & FormArray,
}
}
// Usage example
class ... {
myForm: AbstractFormGroup
...
this.myForm = this.fb.group({...}) as AbstractFormGroup
}