Search code examples
angularangular-ngmodelngmodel

I am getting error in binding ngModel in input


While binding ngModel to the input I am finding the following error -

Can't bind to 'ngModel' since it isn't a known property of 'input'. ("<div>
<span> Select : </span>
<input type="radio" name="options" value="all" [ERROR ->][(ngModel)]="SelectRadioButtonValue" (change)="onRadioButtonSelectionChanged()"  />
<span> {{"All: " "): ng:///AppModule/EmpCountComponent.html@2:47
Can't bind to 'ngModel' since it isn't a known property of 'input'. (">
<span> {{"male: " + '(' + male + ')' }} </span>
<input type="radio" name="options" value="female" [ERROR ->][(ngModel)]="SelectRadioButtonValue"   (change)="onRadioButtonSelectionChanged()" />
<span> {{"Female"): ng:///AppModule/EmpCountComponent.html@6:50

Solution

  • For using [(ngModel)] you should import FormsModule into your AppModule:

    import { FormsModule } from '@angular/forms';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        FormsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })