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

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

我该如何解决这个问题?


当前回答

如果在导入formsmodule之后问题仍然存在,请检查Input是否具有“name”属性,其值等于页面上的另一个输入。

其他回答

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

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

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

[...]

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

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

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

@NgModule({
 imports: [ 
            FormsModule  
          ];

HTML代码:

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

尝试添加

模块级的ngModel

代码与上面的相同

在我的例子中,在我的应用程序的惰性加载转换期间,我在app-routing.module.ts中错误地导入了RoutingModule而不是ComponentModule

以上提到的解决这个问题的方法都是正确的。 但是如果你使用的是惰性加载,FormsModule需要导入到子模块中,因为子模块中有表单。在app.module.ts中添加它不会有帮助。