Today, we'll discuss about the common error in Angular: Can't bind to 'ngmodel' since it isn't a known property of 'input' . The error will occur when we tend to use the two-way data binding using 'ngModel' in our input tag. Well, there's a very simple and quick fix to this error, just by importing the "FormsModule" module in our app.module.ts. Let's go through this blog to see how it works.
What is "Can't bind to 'ngmodel' since it isn't a known property of 'input'" #
As we mentioned above, this error occurs when we try to use ngModel with the input tag but we didn't not import it in our Angular app.module.ts . Let's look at the example below:
In component.html
In component.ts
name: string = "";
This is the error:
Can't bind to 'ngModel' since it isn't a known property of 'input'.ngtsc(-998002)
Can't bind to 'ngModel' since it isn't a known property of 'input'.ngtsc(-998002)
From above example, you may see that we're using two-way data binding (put a banana in a box) which bind between and "name" property in the ts file. The logic and syntax is exactly corerct, but why this error still happens? Let's move to the next section.
Solution #
First, go to your app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Next, import the FormsModule in your app.module.ts:
import { FormsModule } from '@angular/forms';
Finally, put the FormsModule in imports: [ ] of the @NgModule directive:
imports: [
BrowserModule,
AppRoutingModule,
FormsModule
],
With these, we're telling our Angular application that FormsModule is our part of member, and we can use the directives provided by it, such as ngModel . After this, you may see the error disappears and you can continue building your application or use ngModel anywhere in your components for two-way data binding!
The Conclusion #
Great, you must be able to solve this simple error after going through this study blog. In this blog, we've learned what is Can't bind to 'ngmodel' since it isn't a known property of 'input' , its causes and the solution to fix it. Hope your guys enjoy this tutorial and find it helpful. Thanks :)