【发布时间】:2022-04-20 13:05:34
【问题描述】:
我完全没有想法。我想使用 Reactive Forms Module,所以我在 app.module.ts 中导入了它,就像
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
...
],
imports: [
...
ReactiveFormsModule,
...
],
providers: [],
bootstrap: [AppComponent]
})
在我定义的组件中:
import { Component, OnInit} from "@angular/core";
import { FormControl, FormGroup } from '@angular/forms';
@Component({
...
})
export class SearchComponent implements OnInit{
//Variablen
form: FormGroup;
//Konstruktor
constructor(){}
//Methoden
ngOnInit(){
this.form = new FormGroup({
'title': new FormControl(null)
});
}
showValue(){
console.log(this.form.get('title'));
}
}
编译运行良好,但在显示时会崩溃,并在浏览器控制台中显示以下错误: “core.js:6156 ERROR 错误:NG0201:在 NodeInjector 中找不到 NgControl 的提供程序。”
你们中有人知道出了什么问题吗?
如果有任何提示,我将不胜感激。
非常感谢!
【问题讨论】:
-
据我所知,不推荐使用
new FormGroup(...)。注入FormBuilder的实例,然后使用this.fb.group({ control: new FormControl(null)})。也许可以帮助你。
标签: angular typescript web-applications