gpt4 book ai didi

angular - 错误 错误 : NG0201: No provider for NgControl found in NodeInjector

转载 作者:行者123 更新时间:2023-12-04 11:38:13 24 4
gpt4 key购买 nike

我完全没有想法了。我想使用 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 的提供程序。”
你们中有人知道出了什么问题吗?
我真的很感激任何提示。
非常感谢!

最佳答案

要完成这项工作,您必须在@NgModule 中导入 ReactiveFormsModule,正如您的问题所暗示的那样,它是 ViewsModule。因为 FormControl 作为 ReactiveFormsModule 的一部分而不是 FormsModule 公开。

import { ReactiveFormsModule, ... } from '@angular/forms';

@NgModule({
imports: [..., ReactiveFormsModule, ...],
...
})
export class ViewsModule {...}

关于angular - 错误 错误 : NG0201: No provider for NgControl found in NodeInjector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65936059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com