javascript - Angular 4 : Can't bind to 'ngModel'

标签 javascript angular

我刚刚开始学习。我的 app.components.ts 文件如下所示:

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 = '';
}

我的app.component.html是这样的:

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

Javascript 控制台给我以下错误:

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页面建议添加 FormsModule 但如您所见,它不起作用。

最佳答案

您是否缺少AppModule类?

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

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

关于javascript - Angular 4 : Can't bind to 'ngModel' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44875001/

相关文章:

javascript - 使用 Google 日历在 Fullcalendar 中选择事件

javascript - 如何创建 CSS 并将其应用于 javascript Alert

Javascript 正则表达式仅匹配整个 url 作为输入

javascript - XSRF-TOKEN 没有被 Angular 2 发送,因为它说默认情况下会发送

Angular2 获取被点击的元素id

javascript - 是否可以在 angular2 之外设置变量 javascript 变量?

Angular 9 生产错误 : Cannot set property ɵfac of (abstract) class MyFilter { } which has only a getter

javascript - 来自播放列表范围问题的 jQuery YouTube 数组

javascript - 将文本文件拆分为 Node JS 中的数组

angular - 如何将 angular 项目从 vscode 发布到 stackblitz?