我使用的是Angular 4,我在控制台得到了一个错误:

不能绑定到ngModel,因为它不是input的已知属性

我该如何解决这个问题?


当前回答

在我的情况下,我拼写错误,我是指ngmodel而不是ngmodel:)希望它有帮助!

Expected - [(ngModel)] Actual - [(ngmodel)]

其他回答

我尝试了上面提到的所有方法,但仍然不起作用。

但最后我在Angular站点上发现了这个问题。尝试在module中导入formModule。就是这样。

在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

代码与上面的相同

我在用Karma/Jasmine测试Angular 6应用时遇到了这个错误。我已经在顶层模块中导入了FormsModule。但是当我添加一个使用[(ngModel)]的新组件时,我的测试开始失败。在这种情况下,我需要在我的TestBed TestingModule中导入FormsModule。

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      FormsModule
    ],
    declarations: [
      RegisterComponent
    ]
  })
  .compileComponents();
}));

如果双向绑定不起作用,您应该验证以下内容。

在html中ngModel应该这样调用。不依赖于输入元素的其他属性

<输入[(ngModel)] = >“inputText

确保FormsModule被导入到模块文件app.modules.ts中

import { FormsModule } from '@angular/forms';

@NgModule({
    declarations: [
        AppComponent,
        HomeComponent // suppose, this is the component in which you are trying to use two ay binding
    ],
    imports: [
        BrowserModule,
        FormsModule,
        // other modules
],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

方法的声明中添加了你试图使用ngModel进行双向绑定的组件。在前面第2点中添加的代码

这就是使用ngModel使双向绑定工作所需做的所有事情,这在angular 9中都得到了验证