我使用的是Angular 4,我在控制台得到了一个错误:

不能绑定到ngModel,因为它不是input的已知属性

我该如何解决这个问题?


当前回答

为了对表单输入使用双向数据绑定,你需要在Angular模块中导入FormsModule包。

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

@NgModule({
    imports: [
         FormsModule      
    ]

EDIT

因为有很多相同问题的重复问题,我正在加强这个答案。

有两个可能的原因

缺少FormsModule,因此将此添加到您的模块, import {FormsModule} from '@angular/forms'; @NgModule ({ 进口:[ FormsModule ] 检查input标签中[(ngModel)]的语法/拼写

其他回答

为了对表单输入使用双向数据绑定,你需要在Angular模块中导入FormsModule包。

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

@NgModule({
    imports: [
         FormsModule      
    ]

EDIT

因为有很多相同问题的重复问题,我正在加强这个答案。

有两个可能的原因

缺少FormsModule,因此将此添加到您的模块, import {FormsModule} from '@angular/forms'; @NgModule ({ 进口:[ FormsModule ] 检查input标签中[(ngModel)]的语法/拼写

在我的情况下,我拼写错误,我是指ngmodel而不是ngmodel:)希望它有帮助!

Expected - [(ngModel)] Actual - [(ngmodel)]

在app.module.ts中导入表单模块。

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


@NgModule({
  declarations: [
    AppComponent,
    ContactsComponent
  ],
  imports: [
    BrowserModule,HttpModule,FormsModule     //Add here form  module
  ],
  providers: [],
  bootstrap: [AppComponent]
})

在html中:

<input type="text" name="last_name" [(ngModel)]="last_name" [ngModelOptions]="{standalone: true}" class="form-control">

首先导入FormsModule,然后在component.ts中使用ngModel

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

@NgModule({
 imports: [ 
            FormsModule  
          ];

HTML代码:

<input type='text' [(ngModel)] ="usertext" />

如果你想对表单输入使用双向数据绑定,你需要在Angular模块中导入formsmodule包。要了解更多信息,请参阅Angular 2官方教程和表单的官方文档

在app.module.ts中添加以下行:

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

[...]

@NgModule({
  imports: [
    [...]
    FormsModule
  ],
  [...]
})