Angular 2 ngModel双向绑定
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双向绑定问题。
确保导入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.
在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双向数据绑定问题的原因和解决方法。