我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
当前回答
我的答案是ngModel的拼写错误。我把它写成这样:ngModule,而它应该是ngModel。
所有其他尝试显然都未能为我解决这个错误。
其他回答
我在用Karma/Jasmine测试Angular 6应用时遇到了这个错误。我已经在顶层模块中导入了FormsModule。但是当我添加一个使用[(ngModel)]的新组件时,我的测试开始失败。在这种情况下,我需要在我的TestBed TestingModule中导入FormsModule。
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
FormsModule
],
declarations: [
RegisterComponent
]
})
.compileComponents();
}));
尝试添加
模块级的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">
我的答案是ngModel的拼写错误。我把它写成这样:ngModule,而它应该是ngModel。
所有其他尝试显然都未能为我解决这个错误。