395

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?

Jayampathy Wijesena
  • 1,670
  • 1
  • 18
  • 26
Vijay Kumar
  • 4,059
  • 3
  • 11
  • 6
  • 47
    You need to add `FormsModule` to `imports: []` of the module where you use `ngModel`. Otherwise post your code. – Günter Zöchbauer Apr 08 '17 at 17:09
  • 12
    I can't thinking that *all* new Angular 2 & 4 developers are going to hit this exact issue (myself included). When was the last time you used Angular and *didn't* want to use ngModel somewhere ? I don't understand why FormsModule isn't just included by default.... – Mike Gledhill Jun 24 '17 at 06:40
  • 2
    For what it's worth, I encountered this error in IONIC-4 ( 4.11.0 ) when working with Forms Validation. If I do nothing else except add formControlName="myControl" to any anywhere in the form, I get the ngModel binding error message. Alternate properties, like formControlName1="myControl" do not cause this error. – Memetican Mar 14 '19 at 22:56
  • 2
    Possible duplicate of [Can't bind to 'ngModel' since it isn't a known property of 'input'](https://stackoverflow.com/questions/38892771/cant-bind-to-ngmodel-since-it-isnt-a-known-property-of-input) – Heretic Monkey Mar 23 '19 at 16:45

21 Answers21

919

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

Edric
  • 24,639
  • 13
  • 81
  • 91
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 37
    What is the relation between ngModel and FormsModule? However this is not working for me. – Govind Aug 13 '17 at 07:39
  • 8
    FormsModule provides additional directives on form's elements, including input, select, etc. That's why it's required. Don't forget to import FormsModule in the same module which declares your components containing form element bindings. – Bob Sep 21 '17 at 01:32
  • 1
    What do we do if we're using this in a component ts file? – Mike Warren Dec 20 '17 at 15:54
  • 1
    you should not import inside component.ts – Sajeetharan Dec 20 '17 at 15:55
  • @Sajeetharan what if I have a template in my Component that uses ng-model in some form element? – CJBrew Jan 10 '18 at 16:49
  • then also it should be the same – Sajeetharan Jan 10 '18 at 16:50
  • The same as what? – CJBrew Jan 10 '18 at 16:53
  • the answer above, i guess you are refering [(ngModel)] right? or ng-model? – Sajeetharan Jan 10 '18 at 16:54
  • @Sajeetharan.. Hello Sajeetharan .. can you please help me out with this question......https://stackoverflow.com/questions/49535102/deleting-selected-items-from-mat-list-not-working-in-angular-2-using-angular-mat/49541113#49541113 – Heena Mar 29 '18 at 07:32
  • What if I have 2+ `child.module.ts`, should I import `FormModule` separately in each child module or there is a better way to do it? – phougatv May 13 '18 at 10:36
  • `ngModal` dammit – Worthy7 May 25 '18 at 03:59
  • After running into this in two earlier projects you'd think I'd remember this by now. One of those things you do once per project and forget all about.. – Eric Soyke Jul 12 '18 at 18:28
  • I am facing similar issues. Here is my code: File: server.component.ts

    File:app.module.ts import { FormsModule, ReactiveFormsModule } from '@angular/forms' NgModule({ declarations: [ AppComponent, ServerComponent, ServersComponent ], imports: [ FormsModule, BrowserModule ], providers: [], bootstrap: [AppComponent] })
    – RSB Jul 16 '18 at 18:38
  • https://stackoverflow.com/questions/51528680/md-autocomplete-not-found-passing-the-old-values-when-we-submit-the-form – its me Jul 26 '18 at 05:35
  • it's 2019 and we are at Angular 8 and still have these problems... when do they want to fix it?! – Sharif Yazdian Sep 19 '19 at 18:33
  • I do have the same problem, I have the Angular v9 it happens to me. Your answer doesn't work for me :( – TheCoderGuy Jul 01 '20 at 21:31
  • Importing component in the module.ts file solved my problem. I forgot to add component registration. P.s: Importing FormModule in module.ts is must. – Hassan Raza Oct 31 '20 at 20:47
  • "Check the syntax/spelling of [(ngModel)] in the input tag" This is exactly the mistake I made. Thank you for the edit. – Carson Jul 15 '22 at 15:55
37

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.

Abhishek Chokra
  • 1,381
  • 2
  • 9
  • 17
Hatem
  • 403
  • 4
  • 2
18

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-> [].

import { FormsModule } from '@angular/forms';

@NgModule({

   imports: [ FormsModule ],       // HERE

   declarations: [ AppComponent ],
   bootstrap: [ AppComponent ]
 })
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
  • 1
    Well, it depends: if your Angular app consists of more than just the root app module, you might want to import the FormsModule in selected modules only. – MichaelHuelsen Jan 01 '21 at 12:30
13

All the above mentioned solutions to the problem are correct. But if you are using lazy loading, FormsModule needs to be imported in the child module which has forms in it. Adding it in app.module.ts won't help.

Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82
Ramya
  • 225
  • 1
  • 6
  • 15
11

I ran into this error when testing my Angular 6 App with Karma/Jasmine. I had already imported FormsModule in my top-level module. But when I added a new component that used [(ngModel)] my tests began failing. In this case, I needed to import FormsModule in my TestBed TestingModule.

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      FormsModule
    ],
    declarations: [
      RegisterComponent
    ]
  })
  .compileComponents();
}));
Bill
  • 1,407
  • 10
  • 18
6

In app.module.ts add this:

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
    declarations: [AppComponent],
    imports: [FormsModule],
})
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • 9
    What additional value does this answer add to the existing answers? – Günter Zöchbauer Aug 23 '17 at 08:14
  • 4
    doesnt add any value to the answer and the format of the code is wrong. Confusing for begineer. – ssmsnet Sep 11 '17 at 14:28
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Clijsters Dec 28 '17 at 09:02
5

Add FormsModule in your NgModule imports (hence ngModel is a part of FormsModule).

Note it can be AppModule or feature module loaded lazily via lazy loading.

imports: [
   ...,
   FormsModule,
   ...
]
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
4

You should verify following things if the two way binding does not work.

In html the ngModel should be called this way. There is no dependency on other attribute of the input element

<input [(ngModel)]="inputText">

Make Sure FormsModule is imported into the modules file app.modules.ts

import { FormsModule } from '@angular/forms';

@NgModule({
    declarations: [
        AppComponent,
        HomeComponent // suppose, this is the component in which you are trying to use two ay binding
    ],
    imports: [
        BrowserModule,
        FormsModule,
        // other modules
],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Make sure the component in which you are trying to use ngModel for two way binding is added in the declarations of the. Code added in the previous point #2

This is everything that you need to do to make the two way binding using ngModel work, this is validated up to angular 9

Avinash
  • 4,115
  • 2
  • 22
  • 41
4

After spending hours on this issue found solution here

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    
@NgModule({
    imports: [
         FormsModule,
         ReactiveFormsModule      
    ]
})
Singh
  • 163
  • 1
  • 8
3

The Answer for me was wrong spelling of ngModel. I had it written like this : ngModule while it should be ngModel.

All other attempts obviously failed to resolve the error for me.

Dyary
  • 753
  • 7
  • 14
3

enter image description hereI tried all the thing that are mentioned above, but still it is not working.

but finally I found the issue in Angular site.Try to import formModule in module.ts thats it. enter image description here

Arpit Sharma
  • 138
  • 1
  • 11
2

Try adding

ngModel in module level

The code is same as the above

2

Update with Angular 7.x.x, encounter the same issue in one of my modules.

If it lies in your independent module, add these extra modules:

import { CommonModule } from "@angular/common";
import { FormsModule } from "@angular/forms";

@NgModule({
  imports: [CommonModule, FormsModule], // the order can be random now;
  ...
})

If it lies in your app.module.ts, add these modules:

import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

@NgModule({
  imports:      [ FormsModule, BrowserModule ], // order can be random now
  ...
})

A simple demo to prove the case.

Hearen
  • 7,420
  • 4
  • 53
  • 63
2

In my case I added the missing import, which was the ReactiveFormsModule.

S34N
  • 7,469
  • 6
  • 34
  • 43
2

If you want to use two-way data binding for form inputs you need to import theFormsModule package in your Angular module. For more info see the Angular 2 official tutorial here and the official documentation for forms

in the app.module.ts add below lines :

import { FormsModule } from '@angular/forms';

[...]

@NgModule({
  imports: [
    [...]
    FormsModule
  ],
  [...]
})
Swapnil G Thaware
  • 799
  • 3
  • 6
  • 19
2

In my case I misspelled , I was referring as ngmodel istead of ngModel :) Hope It helps!

Expected - [(ngModel)] Actual - [(ngmodel)]

pragapraga
  • 197
  • 1
  • 3
  • 11
1

import form module in app.module.ts.

import { FormsModule} from '@angular/forms';


@NgModule({
  declarations: [
    AppComponent,
    ContactsComponent
  ],
  imports: [
    BrowserModule,HttpModule,FormsModule     //Add here form  module
  ],
  providers: [],
  bootstrap: [AppComponent]
})

In html:

<input type="text" name="last_name" [(ngModel)]="last_name" [ngModelOptions]="{standalone: true}" class="form-control">
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
1

in angular 7, you have to import "ReactiveFormsModule".

import {FormsModule, ReactiveFormsModule} from '@angular/forms';

I solved this issue by this import.It would help you.

  • for some strange reason it only worked with `ReactiveFormsModule` & without `FormsModule` (angular 13) – fredtma Feb 01 '22 at 08:02
0

first import FormsModule and then use ngModel in your component.ts

import { FormsModule } from '@angular/forms';

@NgModule({
 imports: [ 
            FormsModule  
          ];

HTML Code:

<input type='text' [(ngModel)] ="usertext" />
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
0

If even after importing the formsmodule the problem persists, check that your Input does not have a "name" attribute with value equal to another input on page.

Lucas Simões
  • 589
  • 5
  • 10
0

In my case, during a lazy-loading conversion of my application I had incorrectly imported the RoutingModule instead of my ComponentModule inside app-routing.module.ts

Aaron Jordan
  • 2,447
  • 2
  • 15
  • 13