Angular 类型 'AbstractControl' 缺少类型 'FormGroup' 的以下属性:控件
Angular Type 'AbstractControl' is missing the following properties from type 'FormGroup': controls
我正在研究 Angular FormArray,因此我正在研究本教程:https://www.youtube.com/watch?v=aOQ1xFC3amw
不幸的是我不能应用教程。我的 HTML 部分出错了。
我用Angular11
这是我的 HTML 显示的错误消息:
<div [formGroup]="myForm">
<ng-container formArrayName="demoTypes">
<ng-container *ngFor="let demoForm of demoFormArray.controls">
<div [formGroup]="demoForm"><!--formGroup is as error underlined-->
</div>
</ng-container>
</ng-container>
</div>
错误信息是:
Type 'AbstractControl' is missing the following properties from type 'FormGroup': controls, registerControl, addControl, removeControl, and 3 more.
这是我的 TypeScript class:
export class DemoComponent implements OnInit {
myForm!: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.createForm();
}
createForm() {
this.myForm = this.formBuilder.group({
demoTypes: new FormArray([]),
});
this.addDemoTypes();
}
get demoFormArray() {
return this.myForm.controls["demoTypes"] as FormArray;
}
addDemoTypes(): void {
const demoControl = this.formBuilder.group({
isChecked: new FormControl(true),
demoFormControlName: new FormControl("This is the first one"),
});
this.demoFormArray.push(demoControl);
}
}
这里有什么问题?
[编辑]
在 tsconfig.json 中设置 "strictTemplates": false
解决了我的问题。但这是个好主意吗?
"angularCompilerOptions": {
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": false
}
您可以改用formGroupName
<ng-container *ngFor="let demoForm of demoFormArray.controls; let i = index">
<div [formGroupName]="i">
</div>
</ng-container>
我们不会遇到 typescript 错误,因为我们正在绑定到一个数字
禁用strictTemplates
?
不要。启用 strictTemplates
后,您更有可能发现本来可以被监督的问题。我认为把头埋在沙子里永远解决不了问题。你可能没有看到危险,但危险仍然存在
我正在研究 Angular FormArray,因此我正在研究本教程:https://www.youtube.com/watch?v=aOQ1xFC3amw 不幸的是我不能应用教程。我的 HTML 部分出错了。
我用Angular11
这是我的 HTML 显示的错误消息:
<div [formGroup]="myForm">
<ng-container formArrayName="demoTypes">
<ng-container *ngFor="let demoForm of demoFormArray.controls">
<div [formGroup]="demoForm"><!--formGroup is as error underlined-->
</div>
</ng-container>
</ng-container>
</div>
错误信息是:
Type 'AbstractControl' is missing the following properties from type 'FormGroup': controls, registerControl, addControl, removeControl, and 3 more.
这是我的 TypeScript class:
export class DemoComponent implements OnInit {
myForm!: FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.createForm();
}
createForm() {
this.myForm = this.formBuilder.group({
demoTypes: new FormArray([]),
});
this.addDemoTypes();
}
get demoFormArray() {
return this.myForm.controls["demoTypes"] as FormArray;
}
addDemoTypes(): void {
const demoControl = this.formBuilder.group({
isChecked: new FormControl(true),
demoFormControlName: new FormControl("This is the first one"),
});
this.demoFormArray.push(demoControl);
}
}
这里有什么问题?
[编辑]
在 tsconfig.json 中设置 "strictTemplates": false
解决了我的问题。但这是个好主意吗?
"angularCompilerOptions": {
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": false
}
您可以改用formGroupName
<ng-container *ngFor="let demoForm of demoFormArray.controls; let i = index">
<div [formGroupName]="i">
</div>
</ng-container>
我们不会遇到 typescript 错误,因为我们正在绑定到一个数字
禁用strictTemplates
?
不要。启用 strictTemplates
后,您更有可能发现本来可以被监督的问题。我认为把头埋在沙子里永远解决不了问题。你可能没有看到危险,但危险仍然存在