我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
当前回答
如果在导入formsmodule之后问题仍然存在,请检查Input是否具有“name”属性,其值等于页面上的另一个输入。
其他回答
尝试添加
模块级的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">
在我的例子中,在我的应用程序的惰性加载转换期间,我在app-routing.module.ts中错误地导入了RoutingModule而不是ComponentModule
在花了几个小时在这个问题上找到解决方案
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule
]
})
在我的例子中,我添加了缺失的导入,即ReactiveFormsModule。