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

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

我该如何解决这个问题?


当前回答

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

其他回答

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

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

尝试添加

模块级的ngModel

代码与上面的相同

我在用Karma/Jasmine测试Angular 6应用时遇到了这个错误。我已经在顶层模块中导入了FormsModule。但是当我添加一个使用[(ngModel)]的新组件时,我的测试开始失败。在这种情况下,我需要在我的TestBed TestingModule中导入FormsModule。

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      FormsModule
    ],
    declarations: [
      RegisterComponent
    ]
  })
  .compileComponents();
}));

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

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

@NgModule({
    imports: [
         FormsModule      
    ]

EDIT

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

有两个可能的原因

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

在你的NgModule导入中添加FormsModule(因此ngModel是FormsModule的一部分)。

注意,它可以是AppModule,也可以是通过惰性加载加载的特性模块。

imports: [
   ...,
   FormsModule,
   ...
]