Reputation: 161
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
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
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
Reputation: 1
put it into your local module.
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [],
imports: [
FormsModule
]
})
export class ... {}
Upvotes: -1
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
Reputation: 11
Import ReactiveFormsModule
as well in the current module example (login.module.ts
or any other module), not in app.module.ts
:
Upvotes: 1
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
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