我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
当前回答
在app.module.ts中添加以下内容:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent],
imports: [FormsModule],
})
其他回答
你的ngModel没有工作,因为它还不是你的NgModule的一部分。
你必须告诉NgModule你有权在整个应用中使用ngModel,你可以通过在app.module.ts -> imports->[]中添加FormsModule来实现。
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ FormsModule ], // HERE
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
尝试添加
模块级的ngModel
代码与上面的相同
我在用Karma/Jasmine测试Angular 6应用时遇到了这个错误。我已经在顶层模块中导入了FormsModule。但是当我添加一个使用[(ngModel)]的新组件时,我的测试开始失败。在这种情况下,我需要在我的TestBed TestingModule中导入FormsModule。
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
FormsModule
],
declarations: [
RegisterComponent
]
})
.compileComponents();
}));
首先导入FormsModule,然后在component.ts中使用ngModel
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
];
HTML代码:
<input type='text' [(ngModel)] ="usertext" />
我的答案是ngModel的拼写错误。我把它写成这样:ngModule,而它应该是ngModel。
所有其他尝试显然都未能为我解决这个错误。