by Sai gowtham

How to fix the can't bind to 'ngmodel' error in Angular

In this tutorial, we are going to learn about how to solve the Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’ error in an angular app.

When we use a ngModel directive inside the form input field, we will see this type of error in our terminal.

<input type="name" [(ngModel)]="name"/>

Error

ERROR in src/app/app.component.html:3:41 - error NG8002:
Can't bind to 'ngmodel' since it isn't a known property of 'input'.

<input type="name" [(ngmodel)]="name" />
                    ~~~~~~~~~~~~~~~~~~~

  src/app/app.component.ts:6:16
    6   templateUrl: './app.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component AppComponent.

This occurs due to a FormsModule is not injected into the angular application.

Fixing the error

To fix this error, open your app.module.ts file and import the FormsModule from @angular/forms package and add it to the imports array.

app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course