Angular 2 ngModel双向绑定

22 浏览
0 Comments

Angular 2 ngModel双向绑定

我试图测试Angular2的双向绑定,但是一直出现这个错误:无法绑定到 \'ngModel\',因为它不是 \'input\' 的已知属性。我该如何解决这个问题?\n

import { Component } from '@angular/core';
@Component({
  selector: 'impure-pipe',
  template: ` {{ a| calcPipe:b}}`
})
export class PipesAppComponent {
  a: number = 2;
  b: number = 2;
}

0
0 Comments

Angular 2 ngModel双向绑定问题的原因是没有在app.module.ts中添加FormsModule的定义。

解决这个问题的方法是在app.module.ts中添加FormsModule的导入和引用。

具体代码如下:

import { FormsModule } from '@angular/forms';
@NgModule({
  imports: [
    FormsModule
  ],
  ...
})
export class AppModule { }

在上述代码中,我们通过import语句从'@angular/forms'中导入FormsModule,然后在imports数组中添加FormsModule

通过以上操作,我们成功解决了Angular 2 ngModel双向绑定问题。

0
0 Comments

确保导入FormsModule

The issue is that you have not imported the FormsModule into your Angular 2 application. The FormsModule is required in order to use the ngModel directive for two-way data binding.

To solve this issue, you need to import the FormsModule into your Angular 2 application. This can be done by adding the following line of code to your app.module.ts file:

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

After importing the FormsModule, you also need to add it to the imports array in the @NgModule decorator:

imports: [FormsModule]

Once you have imported and added the FormsModule to your application, you will be able to use the ngModel directive for two-way data binding in your Angular 2 templates.

0
0 Comments

在Angular 2中,当使用ngModel指令进行双向数据绑定时,需要先导入FormsModule并将其添加到Angular模块的imports列表中。在app.module.ts文件中是否已经导入了FormsModule呢?

在Angular 2的官方网站上的Template Syntax页面中,有这样一段话:

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

请检查app.module.ts文件中是否已经导入了FormsModule:

import { NgModule } from '/core';
import { BrowserModule }  from '/platform-browser';
import { FormsModule } from '/forms';
import { AppComponent } from './app.component';
@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

以上是解决Angular 2 ngModel双向数据绑定问题的原因和解决方法。

0