user3607282
user3607282

Reputation: 2555

Angular 6 - Can't use ngModel

I'm using Angular 6. And I want to use ngModel like so:

<input type="text" [(ngModel)]="textValue">

On the html I was getting the following error:

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

When I searched I learnt that I have to import FormsModule. So in my package json I added the angular forms like so:

"dependencies": {
        "@angular/core": "^6.0.4",
        "@angular/forms": "^0.2.0"
   ...

Did a npm install and then on the app.module,

I added FormsModule to imports,

imports: [
        CommonModule,
        FormsModule,
    ],

But when I tried importing FormsModule from angular/forms,

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

It says:

Module '"PATH-TO-PROJECT/node_modules/@angular/forms/index"' has no exported member 'FormsModule'

What should I do to get mgModel working on Angular 6?

Upvotes: 5

Views: 10766

Answers (2)

Vijayakumar Mural
Vijayakumar Mural

Reputation: 114

Change in package.json:

"@angular/forms": "0.3.0"

and then npm install.

There is important information is that whenever we use [(ngModel)] in html tags to bind data then we must have to give name to that tag:

otherwise it will throw error: ngModel must have name property.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222702

On your line of package.json:

"@angular/forms": "0.2.0"

change to:

"@angular/forms": "0.3.0"

run again:

npm install

Upvotes: 6

Related Questions