ERROR 错误:NG0201:在 NodeInjector 中找不到 NgControl 的提供程序 [英] ERROR Error: NG0201: No provider for NgControl found in NodeInjector

查看:87
本文介绍了ERROR 错误:NG0201:在 NodeInjector 中找不到 NgControl 的提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全没有想法了.我想使用 Reactive Forms Module,所以我将它导入 app.module.ts 中,如

i ran completly out of ideas. I want to user Reactive Forms Module, so i imported it in app.module.ts like

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

@NgModule({
  declarations: [
    AppComponent,
    ...
  ],
  imports: [
    ...
    ReactiveFormsModule,
    ...
  ],
  providers: [],
  bootstrap: [AppComponent]
})

在我的组件中我定义了:

In my Component i defined:

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 的提供程序."

Compiling works well, but when displaying it it crashes with the error below shown in the Browser Console: "core.js:6156 ERROR Error: NG0201: No provider for NgControl found in NodeInjector."

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

Does anybody of you has an idea what went wrong?

我真的很感激任何提示.

I would really appreciate any hint.

非常感谢!

推荐答案

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

To make this work you'll have to import the ReactiveFormsModule in your @NgModule which is the ViewsModule as your question suggests. As FormControl is exposed as a part of ReactiveFormsModule and NOT the FormsModule.

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

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

这篇关于ERROR 错误:NG0201:在 NodeInjector 中找不到 NgControl 的提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆