第一步 引入FromsModule
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, FormsModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
<div style="text-align:center"> <h1> Welcome to {{ title }}! </h1> <input type="text" [(ngModel)]="inputValue"/> <h1> {{ inputValue }} </h1> </div>
效果图: