繁体   English   中英

Angular 2 ngModel双向绑定

[英]Angular 2 ngModel two way binding

我试图测试Angular2的双向绑定,但我总是得到这个

错误: 因为它不是'input'的已知属性,所以无法绑定到'ngModel'。

我该如何解决?

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;

}

正如它在本页上所述,Angular 2网站上的模板语法

在可以在双向数据绑定中使用ngModel指令之前,我们必须导入FormsModule并将其添加到Angular模块的导入列表中。 在“表单”一章中了解有关FormsModule和ngModel的更多信息。

您是否在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

您是否在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