【问题标题】:Can't bind to 'ngModel' since it isn't a known property of 'input', even doe the FormsModule is importet无法绑定到“ngModel”,因为它不是“input”的已知属性,即使 FormsModule 是 importet
【发布时间】:2021-11-04 12:49:15
【问题描述】:

不知何故,我收到错误“无法绑定到 'ngModel',因为它不是“输入”的已知属性。 甚至 FormsModule 也是 importet。

html (login.component.html):

<input type="text" [(ngModel)] = "username">
<button (click)="login()">LOGIN DO</button>

app.module.ts:

import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
ButtonsComponent,
ButtonPipe
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
FeatureEagerModule,
CoreModule,
HttpClientModule,
LoginComponent
],
providers: [LoginComponent],
bootstrap: [AppComponent]
})
export class AppModule { }

只是为了确保我还在 login.module.ts 中导入了 FormsModule,但即使这样也无法解决错误。

【问题讨论】:

  • 请澄清您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: html angular typescript components ngmodel


【解决方案1】:

LoginComponent 必须在声明中,而不是提供者

declarations: [
AppComponent,
ButtonsComponent,
ButtonPipe,
LoginComponent
],

【讨论】:

    【解决方案2】:

    您的输入是否包含在表单中?试试这个:

    <input type="text" [(ngModel)] = "username" name="username">
    

    当属性 name

    中时,您总是必须将属性 name 放在 ngModel 中

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2016-12-17
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2016-12-26
      • 2017-02-01
      相关资源
      最近更新 更多