简体   繁体   English

Angular 2 ngModel双向绑定

[英]Angular 2 ngModel two way binding

I was trying to test the two way binding of Angular2 but I'm always getting this 我试图测试Angular2的双向绑定,但我总是得到这个

error: Can't bind to 'ngModel' since it isn't a known property of 'input'. 错误: 因为它不是'input'的已知属性,所以无法绑定到'ngModel'。

How can I solve this ? 我该如何解决?

import { Component } from '@angular/core';


@Component({
  selector: 'impure-pipe',
  template: `<input type="text" [(ngModel)]='a'> <p>{{ a| calcPipe:b}}</p>`
})
export class PipesAppComponent {

  a: number = 2;
  b: number = 2;

}

As it says on this page Template Syntax from the Angular 2 web site 正如它在本页上所述,Angular 2网站上的模板语法

Before we can use the ngModel directive in a two-way data binding, we must import the FormsModule and add it to the Angular module's imports list. 在可以在双向数据绑定中使用ngModel指令之前,我们必须导入FormsModule并将其添加到Angular模块的导入列表中。 Learn more about the FormsModule and ngModel in the Forms chapter. 在“表单”一章中了解有关FormsModule和ngModel的更多信息。

Do you have the FormsModule imported in your app.module.ts ? 您是否在FormsModule导入了app.module.ts

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

@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

确保导入FormsModule

Have you added a definition of FormsModule in app.module.ts? 您是否在app.module.ts中添加了FormsModule的定义?

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


@NgModule({
  imports: [
    FormsModule
  ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM