writelines
writelines

Reputation: 161

ERROR Error: NG0201: No provider for NgControl found in NodeInjector

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'));
    }
}

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.

Thanks a lot!

Upvotes: 16

Views: 39512

Answers (8)

svenson95
svenson95

Reputation: 41

For everyone else who get this Error, in my case the problem was a missing formControlName attribute and a missing formGroup as a parent. If you use custom form controls in your project as well, ensure all references - formControlName's and [formGroup]="myFormGroup" - are set properly.

Upvotes: 3

KmanKid
KmanKid

Reputation: 21

I had a similar Problem and imported the FormsModule as suggested, but the NG0201-Error still persisted.

My mistake was the following:

I copy/pasted a part of an component-template previously and had no idea it was still in a subcomponent of the one i was working on.

 <input [...] formControlName="shoppingAmount" [...] />

The problem with this is that if you are not importing the module everything works out fine because angular is just reading it as an html-attribute, but if you are to import the module you now have a formControlName, that is throwing an NG0201-Error.

This leads us to @svenson95's answer.

I solved my error by removing the obsulete code.

Upvotes: 1

Zhengyao Lu
Zhengyao Lu

Reputation: 1

put it into your local module.

import { FormsModule } from '@angular/forms';
@NgModule({
  declarations: [],
  imports: [
    FormsModule
  ]
})
export class ... {}

Upvotes: -1

Asad Ashraf
Asad Ashraf

Reputation: 1645

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 {...}

Upvotes: 24

hmairi halim
hmairi halim

Reputation: 11

Import ReactiveFormsModule as well in the current module example (login.module.ts or any other module), not in app.module.ts:

enter image description here

Upvotes: 1

Carles Ramos
Carles Ramos

Reputation: 354

You can try to add *ngIf="!isLoading" in your form container in the html. In de .ts file add isLoading = true; in top of var declarations and then after your form is created, isLoading = false; Regards.

Upvotes: 1

Harold
Harold

Reputation: 699

Just found my (same) mistake, you have to import ReactiveFormsModule in "local" module... In your case it must be "search-component.module.ts"

Upvotes: 7

zainhassan
zainhassan

Reputation: 1780

import FormsModule as well in app.module.ts

Upvotes: 2

Related Questions