我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
我使用的是Angular 4,我在控制台得到了一个错误:
不能绑定到ngModel,因为它不是input的已知属性
我该如何解决这个问题?
当前回答
在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">
其他回答
在我的例子中,我添加了缺失的导入,即ReactiveFormsModule。
以上提到的解决这个问题的方法都是正确的。 但是如果你使用的是惰性加载,FormsModule需要导入到子模块中,因为子模块中有表单。在app.module.ts中添加它不会有帮助。
首先导入FormsModule,然后在component.ts中使用ngModel
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
];
HTML代码:
<input type='text' [(ngModel)] ="usertext" />
尝试添加
模块级的ngModel
代码与上面的相同
如果在导入formsmodule之后问题仍然存在,请检查Input是否具有“name”属性,其值等于页面上的另一个输入。