ngModel data binding not working

前端 未结 2 687
一个人的身影
一个人的身影 2020-12-22 10:57

I\'m following the Tour of Heroes tutorial and cannot figure out why ngModel is not updating hero.name or if it\'s just not updating the view. I type into the input but the

相关标签:
2条回答
  • 2020-12-22 11:00

    ([ngModel]) should be [(ngModel)]. It's called the bananas-in-a-box-notation ;) (https://www.bennadel.com/blog/3008-two-way-data-binding-is-just-a-box-of-bananas-in-angular-2-beta-1.htm)

    0 讨论(0)
  • 2020-12-22 11:18

    Open AppModule (app.module.ts) and import the FormsModule symbol from the @angular/forms library.

    import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
    

    Then add FormsModule to the @NgModule metadata's imports array, which contains a list of external modules that the app needs.

    imports: [
      BrowserModule,
      FormsModule
    ],
    

    When the browser refreshes, the app should work again. You can edit the hero's name and see the changes reflected immediately in the above the textbox.


    source: angular doc.

    0 讨论(0)
提交回复
热议问题