zoukankan      html  css  js  c++  java
  • 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,
    
    ...
    
    ]
    
    
    --------------------------- 知道的更多,不知道的也更多 ---------------------------
  • 相关阅读:
    SAS学习 day10
    SAS学习 day9
    SAS学习 day8
    Python解释器 发展史
    os. 模块
    字典
    类型1
    计算机编码
    EDA 会议整理
    2020-8-27
  • 原文地址:https://www.cnblogs.com/mryux/p/15477339.html
Copyright © 2011-2022 走看看