Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 936
user3607282 Avatar asked Jun 15 '18 06:06

user3607282


2 Answers

On your line of package.json:

"@angular/forms": "0.2.0"

change to:

"@angular/forms": "0.3.0"

run again:

npm install

like image 126
Sajeetharan Avatar answered Sep 21 '22 19:09

Sajeetharan


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.

like image 44
Vijayakumar Mural Avatar answered Sep 22 '22 19:09

Vijayakumar Mural