2071

I have this simple input in my component which uses [(ngModel)] :

<input type="text" [(ngModel)]="test" placeholder="foo" />

And I get the following error when I launch my app, even if the component is not displayed.

zone.js:461 Unhandled Promise rejection: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'.

Here is the component.ts:

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Intervention } from '../../model/intervention';

@Component({
   selector: 'intervention-details',
   templateUrl: 'app/intervention/details/intervention.details.html',
   styleUrls: ['app/intervention/details/intervention.details.css']
})
    
export class InterventionDetails
{
   @Input() intervention: Intervention;
    
   public test : string = "toto";
}
spaleet
  • 838
  • 2
  • 10
  • 23
Anthony Brenelière
  • 60,646
  • 14
  • 46
  • 58
  • 4
    I'm having the same problem since they updated to rc5 yesterday (interestingly it works for my colleague..) I think @abreneliere is talking about their tutorials - https://angular.io/docs/ts/latest/tutorial/ – PetLahev Aug 11 '16 at 10:43
  • 4
    Yes I referred to the Tour of heroes tutorial because it does use a ngModel. – Anthony Brenelière Aug 12 '16 at 07:43
  • 13
    I'm just staring Angular and saw this error when doing the Tour of Heroes tutorial. Sure enough, they now [call this out in the very next line](https://angular.io/tutorial/toh-pt1#the-missing-formsmodule) and tell you how/why to correct it. – Matt Penner Dec 20 '17 at 08:43
  • It uses the same fix for textarea too – Jun Aug 30 '18 at 18:39
  • 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
  • @MattPenner's comment should be the answer. – Chiwda Apr 23 '20 at 12:33
  • If you get that error message only when running tests, see the related question [angular2 testing: Can't bind to 'ngModel' since it isn't a known property of 'input'](https://stackoverflow.com/questions/39584534/angular2-testing-cant-bind-to-ngmodel-since-it-isnt-a-known-property-of-in) – Raedwald Nov 08 '20 at 14:08
  • This error can also be caused by not importing the module that's attempting to use `ngModel` into appModule. Say you have `secondModule`, and `thirdModule` and you import only `secondModule` into `appModule` and you import `thirdModule` into `secondModule` and then `thirdModule` trys to use `ngModel`, you'll get this error until you import `thirdModule` into `appModule`. – Stack Underflow Jul 12 '21 at 03:41
  • Everyone says that you need the FormsModule in app.module.ts file. I only have one module and it is defined in there but I get the same message. If I remove the ngModel from the input statement it works fine. I am on Angular 12 - has that changed? – tshad Mar 07 '22 at 21:58

49 Answers49

2821

Yes, that's it. In the app.module.ts file, I just added:

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

[...]

@NgModule({
  imports: [
    [...]
    FormsModule
  ],
  [...]
})
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anthony Brenelière
  • 60,646
  • 14
  • 46
  • 58
  • 24
    Actually, I prefer this Answer to Gabriele's (which is just a link). Here, the exact code is presented. Annoyingly, I hit this problem just following John Paps's "Introduction to Angular 2" on PluralSight, and this issue wasn't even mentioned. For them, the 2-way binding just worked. – Mike Gledhill Jun 08 '17 at 12:32
  • 47
    This worked, but only after I imported it into my sub-module. It's not completely clear to beginners that this doesn't get inherited by sub-modules. – adam0101 Feb 14 '18 at 16:34
  • 9
    In my case, My use of braces were wrong. I was using `[{ngModel}]` instead of the correct one: `[(ngModel)]` – Imtiaz Apr 14 '18 at 19:40
  • 41
    For those who find this because of a similar failure with Jasmine, you'll need to add these imports to the `component.spec.ts` file as well. – theMayer Apr 24 '18 at 15:03
  • 5
    And for where to put it (regarding the Jasmine error): https://stackoverflow.com/questions/39584534/angular2-testing-cant-bind-to-ngmodel-since-it-isnt-a-known-property-of-in#answer-39584550 – bagsmode Oct 11 '18 at 18:10
  • 1
    This error does also happen in tests. If that is your problem, you also have to import the `FormsModule` into your TestBed, similar to Anthonys solution: `TestBed.configureTestingModule({ declarations: […Component], imports: [ FormsModule ] })` – rosetree Apr 20 '19 at 15:26
  • @adam0101 because as you may know the FormsModule is used in the component, right? and components have local scope, not global one. that's why you need to imported it there. There are some modules (such as services) that have global scope which don't need to be imported to every nested component's module. – Fouad Boukredine Oct 02 '19 at 14:58
  • 2
    @Imtiaz remember "banana in a box": `[()]` *not* mustaches in a box `[{}]`...gross! https://angular.io/guide/template-syntax – jbobbins Nov 23 '19 at 05:31
  • The FormModule import must be added into the module where you are trying to use the directive ngModel. – Emiliano Schiano Apr 07 '20 at 03:07
  • I am happy to announce this solution is covered by Assistant https://medium.com/@tomaszs2/mapping-stackoverflow-into-real-time-assistance-e627dda88f69 – Tom Smykowski Jul 26 '20 at 08:55
  • I didn't knew that [(ngModel)] is case sensitive, I wasted a lot of time, done everything and in the end it came out that I was using [(ngmodel)] all in small – Muhammad Ashhar Hasan Jan 29 '21 at 14:42
673

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

For more information, see the Angular 2 official tutorial here and the official documentation for forms.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gabriele Ciech
  • 7,805
  • 3
  • 15
  • 15
  • 11
    I have to admit that I start with Angular on Monday (so 4 days ago) and was doing their tutorials so I'm pretty sure I did something wrong but for me importing FormsModule didn't work. Then I added `import { FORM_DIRECTIVES } from '@angular/forms';` and added the `FORM_DIRECTIVES` to directives and yeah my binding is working again (to be clear it was working before rc5 release) – PetLahev Aug 11 '16 at 13:05
  • 3
    Which seems to be related to this http://stackoverflow.com/questions/31623879/angular-2-two-way-binding-using-ng-model-is-not-working – PetLahev Aug 11 '16 at 13:51
  • 2
    @PetLahev the issue you're having with the tutorial is that on August 7th, when you started, the tutorials were running Release Candidate 4. As of August 8th, they are at Release Candidate 5, which has different syntax – AJ Zane Aug 17 '16 at 04:10
  • Imported angular Forms along with FORM_DIRECTIVES saved me.. :) – Basit Munir Aug 30 '16 at 10:42
  • 7
    `FORM_DIRECTIVES` are dead just in case – Toolkit Jan 31 '17 at 11:19
  • 10
    It's worth nothing that, if you are using a SharedModule, you might need to import FormsModule there aswell. – Difinity Feb 14 '17 at 20:23
302

For using [(ngModel)] in Angular 2, 4 & 5+, you need to import FormsModule from Angular form...

Also, it is in this path under forms in the Angular repository on GitHub:

angular / packages / forms / src / directives / ng_model.ts

Probably this is not a very pleasurable for the AngularJS developers as you could use ng-model everywhere anytime before, but as Angular tries to separate modules to use whatever you'd like you to want to use at the time, ngModel is in FormsModule now.

Also, if you are using ReactiveFormsModule it needs to import it too.

So simply look for app.module.ts and make sure you have FormsModule imported in...

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';  // <<<< import it here
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule, FormsModule // <<<< And here
  ],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

Also, these are the current starting comments for Angular4 ngModel in FormsModule:

/**
 * `ngModel` forces an additional change detection run when its inputs change:
 * E.g.:
 * ```
 * <div>{{myModel.valid}}</div>
 * <input [(ngModel)]="myValue" #myModel="ngModel">
 * ```
 * I.e. `ngModel` can export itself on the element and then be used in the template.
 * Normally, this would result in expressions before the `input` that use the exported directive
 * to have and old value as they have been
 * dirty checked before. As this is a very common case for `ngModel`, we added this second change
 * detection run.
 *
 * Notes:
 * - this is just one extra run no matter how many `ngModel` have been changed.
 * - this is a general problem when using `exportAs` for directives!
 */

If you'd like to use your input, not in a form, you can use it with ngModelOptions and make standalone true...

[ngModelOptions]="{standalone: true}"

For more information, look at ng_model in the Angular section here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alireza
  • 100,211
  • 27
  • 269
  • 172
134

You need to import the FormsModule.

Open app.module.ts and add the lines

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

and

@NgModule({
    imports: [
       FormsModule
    ],
})
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PRao
  • 1,439
  • 1
  • 8
  • 2
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 13:50
103

To resolve We must include FormModule in the app.module.ts file to avoid the Can't bind to ngModel as it isn't a known property of the input problem in Angular apps.

The steps are as follows,

Open app.module.ts and add the import line

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

and add FormsModule to imports

@NgModule({
    imports: [
       FormsModule
    ],
})

Figure 1.0 (app.module.ts file)

enter image description here

Tharindu Lakshan
  • 3,995
  • 6
  • 24
  • 44
  • 3
    100% requires the FormsModule to be imported. Fixed the Issue for me. – Sebastian Scholle Jul 08 '21 at 14:27
  • 1
    This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 14:08
  • Importing the Mocked version is enough: MockModule(FormsModule) – gsziszi Jun 02 '23 at 08:33
69

Assuming you have created a new NgModule, say AuthModule dedicated to handling your authentication needs, make sure to import FormsModule in that AuthModule too.

If you'll be using the FormsModule only in the AuthModule then you wouldn't need to import the FormModule in the default AppModule.

So something like this in the AuthModule:

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

import { authRouting } from './auth.routing';
import { LoginComponent, SignupComponent } from './auth.component';

@NgModule({
  imports:      [
    authRouting,
    FormsModule
   ],
  declarations: [
    SignupComponent,
    LoginComponent
  ]
})
export class AuthModule { }

Then forget about importing in AppModule if you don't use the FormsModule anywhere else.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KhoPhi
  • 9,660
  • 17
  • 77
  • 128
  • I needed it at sub-module where I was using the Form features. Higher up the hierarchy was not sufficient. – Zarepheth Nov 29 '18 at 21:33
53

Simple solution: In file app.module.ts -

Example 1

import {FormsModule} from "@angular/forms";
// Add in imports

imports: [
 BrowserModule,
 FormsModule
 ],

Example 2

If you want to use [(ngModel)] then you have to import FormsModule in app.module.ts:

import { FormsModule  } from "@angular/forms";
@NgModule({
  declarations: [
    AppComponent, videoComponent, tagDirective,
  ],
  imports: [
    BrowserModule,  FormsModule

  ],
  providers: [ApiServices],
  bootstrap: [AppComponent]
})
export class AppModule { }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shashwat Gupta
  • 5,071
  • 41
  • 33
  • 39
    Where exactly is the difference in the two examples? – Philipp Meissner Apr 06 '18 at 12:35
  • 1
    in my case, had to import FormsModule on my component.module.ts – ISFO Oct 05 '19 at 11:19
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 13:49
53

Import the FormsModule in those modules where you want to use the [(ngModel)]

enter image description here

Arul
  • 754
  • 11
  • 21
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 13:53
  • @MartinSchneider then how its having this upvotes :) – Arul Aug 24 '22 at 05:02
  • 1
    My guess: the default design by SO that shows more recent and trending answers above older ones and the fact that most users are lazy ;) – Martin Schneider Aug 24 '22 at 09:59
  • is this works? if yes then close the topic :) @MartinSchneider – Arul Aug 25 '22 at 05:06
51

There are two steps you need to follow to get rid of this error:

  1. import FormsModule in your app module
  2. Pass it as value of imports in @NgModule decorator

Basically, file app.module.ts should look like below:

    import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule }   from '@angular/forms';
    import { AppComponent }  from './app.component';
    import {AppChildComponent} from './appchild.component';
    @NgModule({
      imports:      [ BrowserModule,FormsModule ],
      declarations: [ AppComponent, AppChildComponent ],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule { }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
debugmode
  • 936
  • 7
  • 13
  • Thank you! I'd not declared it in the imports section and it was driving me insane when I moved a component into a sub-module. – Richard Mar 04 '17 at 15:49
51

Sometimes, even though we are already imported BrowserModule, FormsModule and other related modules, we still may get the same error.

Then I realized that we need to import them in order, which is missed in my case. So the order should be like BrowserModule, FormsModule, and ReactiveFormsModule.

As per my understanding, feature modules should follow the basic modules of Angular.

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

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule
  ],
  providers: [],
  declarations: [
    AppComponent
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}
Ganesh
  • 5,808
  • 2
  • 21
  • 41
  • 2
    You have ReactiveFormsModule in your imports array, but not in your imports. just saying. – Daniel Methner Jul 20 '20 at 06:08
  • Is there a way to find out the order? i.e. If we have a few more modules to import, how do I confirm the sequence? – MortimerCat Aug 18 '20 at 09:59
  • As per my understanding, Feature Modules should follow the Basic Modules of Angular. In case of Feature Modules sequence, we don't need to worry because our Error cause is not related to them. – Ganesh Aug 18 '20 at 14:03
  • 5
    2021 reporting in, to mention that `FormsModule` **must** be imported after `NgModule`. This error will appear otherwise. This answer is the only one that mentions the importance of the order! – Obsidian Age Jun 19 '21 at 06:13
  • You don't need ReactiveFormsModule when using template driven forms :-> Order irrelevant. – Kieran Ryan Jul 23 '21 at 23:26
39

I am using Angular 7.

I have to import ReactiveFormsModule, because I am using the FormBuilder class to create a reactive form.

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

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ], declarations: []})
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ved_Code_it
  • 704
  • 6
  • 10
36

You need to import FormsModule in your root module if this component is in the root i.e. app.module.ts

Kindly open app.module.ts

Import FormsModule from @angular/forms

Ex:

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

and

@NgModule({
imports: [
   FormsModule
],
})
WapShivam
  • 964
  • 12
  • 20
  • 1
    A slight extension to [PRao's answer](https://stackoverflow.com/questions/38892771/cant-bind-to-ngmodel-since-it-isnt-a-known-property-of-input/52675849#52675849). – Peter Mortensen Dec 17 '20 at 10:18
33

Import FormsModule in you app module.

It would let your application run well.

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {ContactListCopmponent} from './contacts/contact-list.component';
import { FormsModule }   from '@angular/forms';

import { AppComponent }  from './app.component';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent,ContactListCopmponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shashikant Pandit
  • 2,752
  • 22
  • 29
27

I'm using Angular 5.

In my case, I needed to import ReactiveFormsModule too.

File app.module.ts (or anymodule.module.ts):

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

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ],
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Renato Farias
  • 450
  • 6
  • 11
  • `ReactiveFormsModule` is required when you need `FormGroup, FormControl ,Validators` etc. For using `ngModel`, you don't require `ReactiveFormsModule`. – Amit Chigadani Nov 22 '18 at 06:08
  • @AmitChigadani Adding ReactiveFormModule is the only thing that made the error go away in my case. I'm using Angular 7. – Eternal21 May 29 '19 at 00:25
  • Looks like the latest versions of Angular requires the ReactiveFormsModule to be imported for the ngModel to work. I was trained on Angular 7 and the same when I practiced it did not work, because I had the latest @angular/cli in my machine. After adding this ReactiveFormsModule, it worked perfectly. – itsraghz Feb 01 '21 at 07:36
23

If someone is still getting errors after applying the accepted solution, it could be possibly because you have a separate module file for the component in which you want to use the ngModel property in input tag. In that case, apply the accepted solution in the component's module.ts file as well.

Subham Pasari
  • 339
  • 2
  • 5
22

I know this question is about Angular 2, but I am on Angular 4 and none of these answers helped.

In Angular 4 the syntax needs to be

[(ngModel)]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 33,328
  • 25
  • 83
  • 97
19

If you are still getting the error after importing FormsModule correctly then check in your terminal or (windows console) because your project is not compiling (because of another error that could be anything) and your solution has not been reflected in your browser!

In my case, my console had the following unrelated error:

Property 'retrieveGithubUser' does not exist on type 'ApiService'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mruanova
  • 6,351
  • 6
  • 37
  • 55
  • 1
    What is the action to be taken (after checking)? Please respond by [editing your question](https://stackoverflow.com/posts/45333055/edit), not here in comments (***without*** "Edit:", "Update:", or similar). – Peter Mortensen Dec 17 '20 at 09:41
  • Peter, you don't seem to comprehend my answer. When I say "could be anything" means that after checking the error, the universe of actions to be taken are unlimited. My answer has been upvoted 17 times, which means that many developers found my answer complete and there is no need for an edit. – mruanova Dec 17 '20 at 22:39
18

Import FormModule in file app.module

import { FormsModule } from '@angular/forms'; [...] @NgModule({ imports: [ [...] FormsModule ], [...] })
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vaibhavkhot
  • 502
  • 3
  • 3
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 14:00
16

ngModel is coming from FormsModule. There are some cases when you can receive this kind of error:

  1. You didn't import the FormsModule into the import array of modules where your component is declared - the component in which the ngModel is used.
  2. You have import the FormsModule into one module which is inherited of another module. In this case you have two options:
  • let the FormsModule be imported into the import array from both modules:module1 and module2. As the rule: Importing a module does not provide access to its imported modules. (Imports are not inherited)

    Enter image description here

  • declare the FormsModule into the import and export arrays in module1 to be able to see it in model2 also

    Enter image description here

  1. (In some version I faced this problem) You have imported the FormsModule correctly, but the problem is on the input HTML tag. You must add the name tag attribute for the input, and the object bound name in [(ngModel)] must be the same as the name into the name attribute

    Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RazvanParautiu
  • 2,805
  • 2
  • 18
  • 21
16

You need to import the FormsModule in app.module.ts and add the lines

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

The ngModel directive declared in the FormsModule lets you bind controls in your template-driven form to properties in your data model. When you include the directive using the syntax for two-way data binding, [(ngModel)], Angular can track the value and user interaction of the control and keep the view synced with the model.

blueelite
  • 319
  • 2
  • 7
  • 2
    This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 14:09
15

In the module you are willing to use ngModel you have to import FormsModule

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

@NgModule({
  imports: [
    FormsModule,
  ],

})
export class AbcModule { }
Malindu Sandaruwan
  • 1,477
  • 2
  • 13
  • 27
  • I was missing this in my app.module.ts, thank you Malindu! – Omzig Apr 09 '19 at 15:13
  • 1
    This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 13:49
14

In ngModule you need to import FormsModule, because ngModel is coming from FormsModule. Please modify your app.module.ts like the below code I have shared

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
    declarations: [
         AppComponent,
         HomeComponent
    ],
    imports: [
         BrowserModule,
         AppRoutingModule,
         FormsModule
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 13:51
12

I faced the same problem and the reason was that I was using ngModel in my MenuComponent. I imported my MenuComponent in app.module.ts, but I forgot to declare it.

Error

Declaring the MenuComponent solved my issue. I.e., as shown in the below image:

Solved issue

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmad Habib
  • 2,053
  • 1
  • 13
  • 28
11

For my scenario, I had to import both [CommonModule] and [FormsModule] to my module

import { NgModule } from '@angular/core' 
import { CommonModule } from '@angular/common'; 
import { FormsModule } from '@angular/forms'; 

import { MyComponent } from './mycomponent'

@NgModule({
    imports: [
        CommonModule,
        FormsModule
    ],
    declarations: [
        MyComponent 
    ]
 }) 
export class MyModule { }
Mina Matta
  • 1,024
  • 14
  • 22
11

ngModel should be imported from @angular/forms because it is the part of FormsModule. So I advice you to change your app.module.ts in something like this:

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

[...]

@NgModule({
  imports: [
    [...]
    FormsModule
  ],
  [...]
})
Chris
  • 806
  • 1
  • 10
  • 17
  • 2
    How is this different from previous answers? – Peter Mortensen Dec 17 '20 at 12:38
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 14:01
11

You need to import the FormsModule.

#Open app.module.ts
#add the lines

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

@NgModule({
    imports: [
       FormsModule   <--------add this 
    ],
})


if you are using reactive form then also added  ReactiveFormsModule
Naeem Bashir
  • 1,937
  • 20
  • 17
9

Sometimes you get this error when you try to use a component from a module, which is not shared, in a different module.

For example, you have 2 modules with module1.componentA.component.ts and module2.componentC.component.ts and you try to use the selector from module1.componentA.component.ts in a template inside module2 (e.g. <module1-componentA [someInputVariableInModule1]="variableFromHTTPRequestInModule2">), it will throw the error that the someInputVariableInModule1 is not available inside module1.componentA.component.ts - even though you have the @Input() someInputVariableInModule1 in the module1.componentA.

If this happens, you want to share the module1.componentA to be accessible in other modules. So if you share the module1.componentA inside a sharedModule, the module1.componentA will be usable inside other modules (outside from module1), and every module importing the sharedModule will be able to access the selector in their templates injecting the @Input() declared variable.

Guntram
  • 961
  • 14
  • 19
  • 1
    I have this exact problem, and you say, you should share the module so you can use componentA in ComponentC. But.. How do I share the module1? Which is the sintax to "share a module"? I guessed exporting module1 and then importing it in the module2 was enough sharing, but still having this problem – Agorilla Sep 05 '17 at 11:25
  • 2
    So to answer myself, this is how you share a module https://angular-2-training-book.rangle.io/handout/modules/shared-di-tree.html – Agorilla Sep 05 '17 at 12:06
  • 2
    Hey sorry i didn't see your comment that fast :) Yes, this is how you can create a SharedModule and import your own modules there, which you want to use in multiple other modules. Then you import the SharedModule where you want to use the module which is imported in the shared one. – Guntram Sep 05 '17 at 13:02
9
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule     
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
William Pourmajidi
  • 967
  • 11
  • 12
8

This is for the folks who use plain JavaScript instead of Type Script. In addition to referencing the forms script file on top of the page like below:

<script src="node_modules/@angular/forms/bundles/forms.umd.js"></script>

You should also tell the the module loader to load the ng.forms.FormsModule. After making the changes my imports property of NgModule method looked like below:

imports: [ng.platformBrowser.BrowserModule, ng.forms.FormsModule],

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James Poulose
  • 3,569
  • 2
  • 34
  • 39
7

I upgraded from RC1 to RC5 and received this error.

I completed my migration (introducing a new app.module.ts file, changing package.json to include new versions and missing modules, and finally changing my main.ts to boot accordingly, based on the Angular2 quick start example).

I did an npm update and then an npm outdated to confirm the versions installed were correct, still no luck.

I ended up completely wiping the node_modules folder and reinstalling with npm install - Voila! Problem solved.

Rots
  • 5,506
  • 3
  • 43
  • 51
6

For me the issue was, I forgot to declare the component in the declarations array of the module.

@NgModule({
 declarations: [yourcomponentName]
})
Kiran Kadam
  • 81
  • 1
  • 2
5

When I first did the tutorial, main.ts looked slightly different from what it is now. It looks very similar, but note the differences (the top one is correct).

Correct:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

Old tutorial code:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
Jordan Lapp
  • 982
  • 9
  • 19
5

Actually in most of the cases the FormsModule was already imported. So what you have to do is you have to make sure that

  1. Your component is properly added in the declarations array in app.module.ts file.
  2. You should verify that the binding was well spelled. The correct spelling is [(ngModel)]
Andre TOKAM
  • 183
  • 1
  • 7
5

Combining all the possible solutions :

  1. Check if your typing it correctly. It should be "[(ngModel)]" with "M" capital.
  2. Check if you have import FormsModule in your TS file.If not, you can do it like this :

In app.module.ts, add these lines

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

@NgModule({
  declarations: [
  ],
  imports: [
    xyz,
    abc,
    FormsModule
  ],
})
shekhar chander
  • 600
  • 8
  • 14
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) and others. – Martin Schneider Aug 23 '22 at 14:10
  • @MartinSchneider I've already mentioned on the top. I've compiled them for easier access and save some time :) – shekhar chander Aug 25 '22 at 04:58
4

For any version from Angular 2, you need to import FormsModule in your app.module.ts file and it will fix the issue.

Sidd Thota
  • 2,040
  • 1
  • 20
  • 24
  • This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 13:50
4

Recently I found that mistake happened not only in case when you forgot to import FormsModule in your module. In my case problem was in this code:

<input type="text" class="form-control" id="firstName"
                   formControlName="firstName" placeholder="Enter First Name"
                   value={{user.firstName}} [(ngModel)]="user.firstName">

I fix it with change formControlName -> name

<input type="text" class="form-control" id="firstName"
                   name="firstName" placeholder="Enter First Name"
                   value={{user.firstName}} [(ngModel)]="user.firstName">
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikita Sychou
  • 189
  • 1
  • 11
  • 1
    Yes Nikita the "name" property is also required for the template-driven approach to forms. The name property is used by the angular form directive to assign validation errors, for example. – Kieran Ryan Jul 23 '21 at 23:34
4

Though the answer solves the issue, I would like to provide a little more information on this topic as I have also undergone this issue when I started to work on Angular projects.

A beginner should understand that there are two main types of forms. They are Reactive forms and Template-driven forms. From Angular 2 onward, it is recommended to use Reactive forms for any kind of forms.

Reactive forms are more robust: they're more scalable, reusable, and testable. If forms are a key part of your application, or you're already using reactive patterns for building your application, use reactive forms.

Template-driven forms are useful for adding a simple form to an application, such as an email list signup form. They're easy to add to an application, but they don't scale as well as reactive forms. If you have very basic form requirements and logic that can be managed solely in the template, use template-driven forms.

Refer Angular documents for more details.

Coming to the question, [(ngModel)]="..." is basically a binding syntax. In order to use this in your component HTML page, you should import FormsModule in your NgModule (where your component is present). Now [(ngModel)] is used for a simple two-way binding or this is used in your form for any input HTML element.

On the other hand, to use reactive forms, import ReactiveFormsModule from the @angular/forms package and add it to your NgModule's imports array.

For example, if my component HTML content does not have [(ngmodel)] in any HTML element, then I don't need to import FormsModule.

In my below example, I have completely used Reactive Forms and hence I don't need to use FormsModule in my NgModule.

Create GroupsModule:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GroupsRoutingModule, routedAccountComponents } from './groups-routing.module';
import { ReactiveFormsModule } from '@angular/forms';
import { SharedModule } from '../shared/shared.module';
import { ModalModule } from 'ngx-bootstrap';
@NgModule({
    declarations: [
        routedAccountComponents
    ],
    imports: [
        CommonModule,
        ReactiveFormsModule,
        GroupsRoutingModule,
        SharedModule,
        ModalModule.forRoot()
    ]
})
export class GroupsModule {
}

Create the routing module (separated to main the code and for readability):


import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { GroupsComponent } from './all/index.component';
import { CreateGroupComponent } from './create/index.component';

const routes: Routes = [
  {
    path: '',
    redirectTo: 'groups',
    pathMatch: 'full'
  },
  {
    path: 'groups',
    component: GroupsComponent
  }
];


@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class GroupsRoutingModule { }


export const routedAccountComponents = [
  GroupsComponent,
  CreateGroupComponent
];

CreateGroupComponent html code

<form [formGroup]="form" (ngSubmit)="onSubmit()">
        <label for="name">Group Name</label>
        <div class="form-group">
            <div class="form-line">
                <input type="text" formControlName="name" class="form-control">
            </div>
        </div>
    </form>

CreateGroupComponent ts file


import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { DialogResult } from 'src/app/shared/modal';
import { FormGroup, FormControl, AbstractControl } from '@angular/forms';
import { Subject } from 'rxjs';
import { ToastrService } from 'ngx-toastr';
import { validateAllFormFields } from 'src/app/services/validateAllFormFields';
import { HttpErrorResponse } from '@angular/common/http';
import { modelStateFormMapper } from 'src/app/services/shared/modelStateFormMapper';
import { GroupsService } from '../groups.service';
import { CreateGroup } from '../model/create-group.model';

@Component({
    selector: 'app-create-group',
    templateUrl: './index.component.html',
    providers: [LoadingService]
})

export class CreateGroupComponent implements OnInit {
    public form: FormGroup;
    public errors: string[] = [];
    private destroy: Subject<void> = new Subject<void>();

    constructor(
        private groupService: GroupsService,
        private toastr: ToastrService
    ) { }

    private buildForm(): FormGroup {
        return new FormGroup({
            name: new FormControl('', [Validators.maxLength(254)])
        });
    }

    private getModel(): CreateGroup {
        const formValue = this.form.value;
        return <CreateGroup>{
            name: formValue.name
        };
    }

    public control(name: string): AbstractControl {
        return this.form.get(name);
    }

    public ngOnInit() {
        this.form = this.buildForm();
    }


    public onSubmit(): void {

         if (this.form.valid) {
             // this.onSubmit();
             //do what you need to do
         }
    }
}

I hope this helps developers to understand as to why and when you need to use FormsModule.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Karthik
  • 426
  • 4
  • 7
4

Main reason of this error is you forget to import FormsModule in your app.module.ts.

But sometimes in big projects you may forget add your component in its module and confront this error.

3

I was getting the same error when running my Angular tests because FormsModule was not added in the specification file.

We need to add it in all specification files whereas for the application to run successfully we will add at one place in the app.module.ts file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vishwa G
  • 573
  • 1
  • 6
  • 13
3

This error will happen too even if the FormsModule is imported directly or indirectly (from a shared module for example) in the feature module, if the imported component is not declared on declarations:

Enter image description here

I followed Deborah Kurata's Angular Routing course. While I added the imported component ProductEditInfoComponent on Angular Route's component property, I forgot to add ProductEditInfoComponent on declarations property.

Adding the ProductEditInfoComponent on the declarations property would solve the NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'. problem:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Buen
  • 38,643
  • 9
  • 94
  • 118
3

Make sure for the template-driven forms approach (https://angular.io/guide/forms), as has already been mentioned by @Nikita Sychou, you have decorated the input field with the "name" property, eg

<form>
   <input [(ngModel)]="form.email" name="email"></input>

and that the FormsModule has been imported in the associated @NgModule. Both are preconditions for avoiding the "Can't bind to ngModel" errors. If you still get styling errors in your IDE e.g. IntelliJ complaining about the ngModel directive just ignore them.. you are good to go :-)

Kieran Ryan
  • 593
  • 2
  • 8
  • 18
1

Angular 14+ Using Standalone Component

Import the form component directly into your standalone component:

import { FormsModule, NgForm } from '@angular/forms';
import { NgFor } from "@angular/common";

@Component({
  selector: 'app-table-view',
  standalone: true,
  imports: [ 
    NgFor,
    FormsModule
    ],  
  templateUrl: './table-view.component.html',
  styleUrls: ['./table-view.component.css']
})
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
0

Note: Remember that the ngModel directive is defined as a part of Angular FormsModule and you need to include FormsModule in the imports: [...] section of the Angular module metadata, in which you want to use it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Harsha Vardhan Chakka
  • 1,357
  • 1
  • 9
  • 7
  • 1
    This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 14:01
0

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

@NgModule({
  imports: [

    FormsModule
  ],

})
Abid
  • 117
  • 2
  • 11
0

I am using Angular 9 and I have a '<form' tag with the [formGroup] property. I also had an '<input' tag specifying a button, which caused the error on the [(ngModel)] part.

I resolved the error by just removing only that (ngModel) part.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tonderaimuchada
  • 215
  • 2
  • 6
0

Before you can use two way binding in Angular you need to import the FormsModule package into your module.

You do this by adding FormsModule to the imports array inside your module (e.g. app.module.ts) like this.

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

@NgModule({
  imports: [
    ...,
    FormsModule     <-------
  ],
  [...]
})
David Storm
  • 920
  • 1
  • 9
  • 14
  • 1
    This answer adds no further value over the answers from 2016: [Answer #1](https://stackoverflow.com/a/38894463/1951524), [Answer #2](https://stackoverflow.com/a/38896469/1951524), [Answer #3](https://stackoverflow.com/a/39676661/1951524) – Martin Schneider Aug 23 '22 at 14:08
0

Make sure [(ngModel)] is actually spelled 'ngModel' with Capital M.. I lost some minutes for not seeing this

Marvin Zumbado
  • 1,005
  • 1
  • 9
  • 10
0

It's a famous error in Angular occurs in Angular when you are trying to use the "ngModel" directive on an "input" element, but the "ngModel" directive is not recognized as a property of the "input" element. The solution, is to import the "FormsModule" in your application and include it in the "imports" array of your module. like this:

import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule,
// other imports
],
// other properties
})
export class AppModule { }
Aymen Ragguem
  • 156
  • 2
  • 15
-1

I was getting the same error even that I added FormsModule as described above. So just a simple restart was required to finish the job.