错误错误:NG0201:在 NodeInjector 中找不到 NgControl 的提供者

问题描述 投票:0回答:7

我完全没有主意了。我想使用反应式表单模块,所以我将其导入到 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 错误错误:NG0201:在 NodeInjector 中找不到 NgControl 的提供程序。”

你们中有人知道出了什么问题吗?

我真的很感激任何提示。

非常感谢!

angular typescript web-applications
7个回答
19
投票

要完成此工作,您必须在 ReactiveFormsModule

 中导入 
@NgModule
 ,正如您的问题所示,这是 ViewsModule 。由于 FormControl 作为 ReactiveFormsModule 的一部分而不是 FormsModule 公开。

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

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

5
投票

刚刚发现我的(同样的)错误,你必须在“本地”模块中导入

ReactiveFormsModule
... 在你的情况下它必须是“search-component.module.ts”


2
投票

在 app.module.ts 中导入 FormsModule


1
投票

您可以尝试在 html 的表单容器中添加 *ngIf="!isLoading" 。 在 de .ts 文件中添加 isLoading = true;在 var 声明的顶部,然后在创建表单后, isLoading = false; 问候。


1
投票

在当前模块示例(

ReactiveFormsModule
或任何其他模块)中导入
login.module.ts
,而不是在
app.module.ts
中:

enter image description here


1
投票

对于遇到此错误的其他人,就我而言,问题是缺少 formControlName 属性和缺少 formGroup 作为父项。如果您也在项目中使用自定义表单控件,请确保所有引用 - formControlName 和 [formGroup]="myFormGroup" - 均已正确设置。


-1
投票

将其放入本地模块中。

import { FormsModule } from '@angular/forms';
@NgModule({
  declarations: [],
  imports: [
    FormsModule
  ]
})
export class ... {}
© www.soinside.com 2019 - 2024. All rights reserved.