【发布时间】:2022-11-10 01:58:07
【问题描述】:
我用一些 formControls 创建了 formGroup。我有一个错误,因为我还没有初始化。我该如何解决?
export class AddFormComponent implements OnInit {
addForm: FormGroup; (here is my problem. when i try to add "?" i have an error in html saying addForm<any> | undefined is not assignable to type 'FormGroup<any>)
onSubmit(){
console.log(this.addForm);
}
ngOnInit(): void {
this.addForm = new FormGroup({
productName: new FormControl(),
.....
});
}
【问题讨论】:
-
试试 addForm! :表格组;
-
我解决了。只是将所有内容从 ngOnInit 移动到构造函数。谢谢
-
好的。这将解决这个警告,但从长远来看可能不是正确的方法。
-
它与新特性 Angular 14 Typed Forms 有关
标签: angular typescript forms angular-reactive-forms formgroups