简体   繁体   English

Angular ngModel 未绑定数据

[英]Angular ngModel is not binding to data

Why does two way binding not work??为什么双向绑定不起作用? I am importing the forms module and using the banana syntax.我正在导入 forms 模块并使用香蕉语法。 I've also tried reactive forms and that did not work either.我也尝试过反应式 forms ,但也没有用。 It's not populating the input field with the pre-defined value or updating it when the user interacts with this field.当用户与此字段交互时,它不会使用预定义的值填充输入字段或更新它。

app.module.ts app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    AppRoutingModule,
    CoreModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
})

general.component.ts通用组件.ts

  user = {
    firstName: 'john',
  }

general.component.html general.component.html

  <form>
      <label for="firstName">First Name</label>
      <input
        type="text"
        class="form-control"
        name="firstName"
        id="firstName"
        [(ngModel)]="user.firstName"
      />
  </form>

As i see from your app.module.ts, your general.component.ts does not belong to this module.正如我从您的 app.module.ts 中看到的,您的 general.component.ts 不属于该模块。 (in declarations only app.component.ts) (仅在 app.component.ts 声明中)

Find module, where you have declarated GeneralComponent, and import there forms module找到声明了 GeneralComponent 的模块,并在那里导入 forms 模块

@NgModule({
  declarations: [..., GeneralComponent],
  imports: [..., FormsModule]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM