Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular error: "Can't bind to 'ngModel' since it isn't a known property of 'input'"

I'm using Angular 4 and I am getting an error in the console:

Can't bind to 'ngModel' since it isn't a known property of 'input'

How can I resolve this?

like image 760
Vijay Kumar Avatar asked Apr 08 '17 17:04

Vijay Kumar


People also ask

Can't bind to ngModel since it isn't a known property?

To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.

Can't bind to property since it isn't a known property of?

This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application sub-module but forgot to export it.

Can't bind to ngModel since it isn't a known property of mat select?

Your ngModel is not working because it's not a part of your NgModule yet. You have to tell the NgModule that you have authority to use ngModel throughout your app, You can do it by adding FormsModule into your app. module. ts -> imports -> [] .

What does [( ngModel )] mean?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form.


2 Answers

In order to use two-way data binding for form inputs you need to import the FormsModule package in your Angular module.

import { FormsModule } from '@angular/forms';  @NgModule({     imports: [          FormsModule           ] 

EDIT

Since there are lot of duplicate questions with the same problem, I am enhancing this answer.

There are two possible reasons

  • Missing FormsModule, hence Add this to your Module,

    import { FormsModule } from '@angular/forms';  @NgModule({     imports: [         FormsModule           ] 
  • Check the syntax/spelling of [(ngModel)] in the input tag

like image 122
Sajeetharan Avatar answered Sep 24 '22 06:09

Sajeetharan


This is a right answer. you need to import 'FormsModule'

first in app.module.ts

**

import { BrowserModule } from '@angular/platform-browser'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { NgModule  } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @NgModule({   declarations: [     AppComponent   ],   imports: [     FormsModule,     ReactiveFormsModule ,     BrowserModule,     AppRoutingModule   ],   providers: [],   bootstrap: [AppComponent] }) export class AppModule { } 

** second in app.component.spec.ts

import { TestBed, async } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { AppComponent } from './app.component'; import { FormsModule } from '@angular/forms'; describe('AppComponent', () => {   beforeEach(async(() => {     TestBed.configureTestingModule({       imports: [         RouterTestingModule,         FormsModule       ],       declarations: [         AppComponent       ],     }).compileComponents();   })); 

Best regards and hope will be helpful.

like image 25
Hatem Avatar answered Sep 22 '22 06:09

Hatem