'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!
Solution 1:[1]
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 {...}
Solution 2:[2]
Just found my (same) mistake, you have to import ReactiveFormsModule
in "local" module...
In your case it must be "search-component.module.ts"
Solution 3:[3]
import FormsModule as well in app.module.ts
Solution 4:[4]
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.
Solution 5:[5]
Import ReactiveFormsModule
as well in the current module example (login.module.ts
or any other module), not in app.module.ts
:
Solution 6:[6]
put it into your local module.
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [],
imports: [
FormsModule
]
})
export class ... {}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Harold |
Solution 3 | zainhassan |
Solution 4 | Carles Ramos |
Solution 5 | Cristik |
Solution 6 | Zhengyao Lu |