1-1app.modult.ts中配置

  1. //1.在app.module.ts根目录中导入并注册
  2. import {FormsModule} from '@angular/forms'
  3. @NgModule({
  4. ...
  5. imports: [
  6. ...
  7. FormsModule //注册
  8. ],
  9. ...
  10. })

1-2在组件中使用

  1. //.component.ts
  2. export class HeaderComponent implements OnInit {
  3. public msg:string='hello world';
  4. ...
  5. }
  6. }
  7. //.html
  8. <input type="text" [(ngModel)]="msg"> // [(ngModel)]="msg" 快捷键ng就可以出来