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,
    
    ...
    
    ]
    
    
    --------------------------- 知道的更多,不知道的也更多 ---------------------------
  • 相关阅读:
    [树形DP]Luogu P1131 [ZJOI2007]时态同步
    [状压DP]JZOJ 1303 骑士
    [DFS]JZOJ 1301 treecut
    [最小费用最大流]JZOJ 4802 探险计划
    [KMP][倍增求LCA]JZOJ 4669 弄提纲
    [DP]JZOJ 1758 过河
    列表生成式和生成器表达式
    协程函数
    生成器
    迭代器
  • 原文地址:https://www.cnblogs.com/mryux/p/15477339.html
Copyright © 2011-2022 走看看