Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 503
writelines Avatar asked Jan 28 '21 11:01

writelines


3 Answers

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 {...}
like image 62
Asad Ashraf Avatar answered Oct 16 '22 15:10

Asad Ashraf


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

like image 5
Harold Avatar answered Oct 16 '22 15:10

Harold


import FormsModule as well in app.module.ts

like image 2
zainhassan Avatar answered Oct 16 '22 15:10

zainhassan