Cody
Cody

Reputation: 2649

Angular 4: Can't bind to 'ngModel'

I have just started learning. My app.components.ts file looks like this:

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

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  // title = 'app';
  name = '';
}

and my app.component.html is this:

  <input type="text" [(ngModel)]="name">
  <p>{{ name }}</p>

Javascript console is giving me following error:

compiler.es5.js:1689 Uncaught Error: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("et="_blank" href="http://angularjs.blogspot.ca/">Angular blog</a></h2>
  </li>
  <input type="text" [ERROR ->][(ngModel)]="name">
  <p>{{ name }}</p>
</ul>
"): ng:///AppModule/AppComponent.html@18:21
    at syntaxError (http://localhost:4200/vendor.bundle.js:18052:34)
    at TemplateParser.webpackJsonp.../../../compiler/@angular/compiler.es5.js.TemplateParser.parse (http://localhost:4200/vendor.bundle.js:29164:19)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileTemplate (http://localhost:4200/vendor.bundle.js:43209:39)
    at http://localhost:4200/vendor.bundle.js:43129:62
    at Set.forEach (native)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileComponents (http://localhost:4200/vendor.bundle.js:43129:19)

...

This page recommends adding FormsModule but as you can see it's not working.

Upvotes: 2

Views: 1437

Answers (2)

DeborahK
DeborahK

Reputation: 60518

Are you missing the class AppModule?

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

@NgModule({
  imports:   [FormsModule ],
  //...
})
export class AppModule {} // <-- do you have this part?

Upvotes: 2

Tiago Pereira
Tiago Pereira

Reputation: 29

ngModel needs name into input:

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

Upvotes: 0

Related Questions