我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
当前回答
在我的情况下,我拼写错误,我是指ngmodel而不是ngmodel:)希望它有帮助!
Expected - [(ngModel)] Actual - [(ngmodel)]
其他回答
这是一个正确答案。 你需要导入FormsModule
首先在app.module.ts中
**
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
FormsModule,
ReactiveFormsModule ,
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
** 在app.component.spec.ts中的第二个
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FormsModule
],
declarations: [
AppComponent
],
}).compileComponents();
}));
最好的问候,希望会有所帮助。
我尝试了上面提到的所有方法,但仍然不起作用。
但最后我在Angular站点上发现了这个问题。尝试在module中导入formModule。就是这样。
在app.module.ts中添加以下内容:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent],
imports: [FormsModule],
})
尝试添加
模块级的ngModel
代码与上面的相同
在我的例子中,我添加了缺失的导入,即ReactiveFormsModule。