angular之双向绑定

html中使用ngModel

// login.component.html
<input id="username" type="text" class="form-control" [(ngModel)]="username">
<input id="password" type="password" class="form-control" [(ngModel)]="password">

ts中使用@Input

// login.component.ts
@Input() username: string;
@Input() password: string;

login(){
    const myObserver = {
      next: (data: LoginResult) => localStorage.token = data.sessionToken,
      complete: () => {
        console.log('Login successful');
        this.router.navigate(['/overview)']);
      },
      error: () => {
        console.log('Login failed');
      }
    };

    this.authService.login(this.username, this.password).subscribe((myObserver));
  }

ng build会出错,需要把FormsModule加上。

// app.module.ts
import { FormsModule } from '@angular/forms';

imports: [
BrowserAnimationsModule,
FormsModule,

...

]

上一篇:706 div2


下一篇:[2019 icpc徐州] H.Yuuki and a problem 带修改的主席树(主席树+树状数组)