【问题标题】:mat-checkbox check only if condition is truemat-checkbox 仅在条件为真时检查
【发布时间】:2018-05-21 11:08:46
【问题描述】:

我有一个带有mat-checkbox 的用户表列表。当mat-checkbox被点击时,只有在某个条件为真时才应该勾选复选框。

在这段代码中,我试图只检查id=2 的复选框。但是正如您所看到的,最初当单击2 的复选框时,即使将2 添加到应检查的元素数组中,它也不会被检查。

另一方面,当1,3 or 4 的复选框被点击时,它们会被选中。

component.html

<tbody>
    <tr *ngFor="let user of users; let in = index">
            <td>
                <mat-checkbox [ngModel]="canbeChecked(user.id)" (click)="userSelectClick(user.id)"></mat-checkbox>
            </td>
    </tr>
</tbody>

组件.ts

export class AppComponent {

  users = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
  checked: boolean = true
  constructor() { }
  selected: any = [];

  canbeChecked(id): boolean {
    console.log("check " + id)
    console.log(this.selected)
    return this.selected.includes(id);

  }

  userSelectClick(id) {

    if (!this.selected.includes(id)) {
      if (id == 2)
        this.selected.push(id);
      console.log("select " + id)
      console.log(this.selected)
    }
    else {
      this.selected = this.selected.filter(item => item !== id)
    }

  }

}

工作代码:https://stackblitz.com/edit/mat-checkbox-conditional

【问题讨论】:

  • 请查看我的代码.. 我在ngModel 上使用了事件绑定,以便在点击事件时更新。

标签: angular typescript angular-material2


【解决方案1】:

我在 html 中对 ngModel 使用了事件绑定,并且还修改了 app.component.ts 中的一些代码。现在只能选中 id 为 2 的复选框。请看下面的代码

在 app.component.html 中

<mat-checkbox (ngModel)="canbeChecked(user.id)" (click)="userSelectClick(user.id)"></mat-checkbox>

在 app.component.ts 中

export class AppComponent {

  users = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
  checked: boolean = true
  constructor() { }
  selected: any = [];

  canbeChecked(id): boolean {
    console.log(this.selected.includes(id));
    return this.selected.includes(id);
  }

  userSelectClick(id) {

    if (!this.selected.includes(id)) {
      if (id == 2){
         this.selected.push(id);
      }
      else{
         return false;
      }
    }
    else {
      if(id == 2){
        this.selected = [];
      }
      else
       return false;
    }
  }

}

这是一个有效的链接

https://stackblitz.com/edit/mat-checkbox-conditional-z8uc94

【讨论】:

    【解决方案2】:

    我是 stackoverflow 的新手。您可以尝试更改这些内容的事件。

    组件.ts

    import { Component } from '@angular/core';
    
    
    @Component({
      selector: 'material-app',
      templateUrl: 'app.component.html',
      styleUrls: ['app.component.css']
    })
    export class AppComponent {
    user_checked = false;
      users = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
      checked: boolean = false;
      constructor() { }
      selected: any = [];
    
      canbeChecked(id): boolean {
        console.log("check " + id)
        console.log(this.selected)
        return this.selected.includes(id);
    
      }
    
      userSelectClick(event,id) {
        if (!this.selected.includes(id)) {
          if (id == 2)
            this.selected.push(id);
          console.log("selecrted iDs " + id)
          console.log(this.selected)
        }
        else {
          this.selected = this.selected.filter(item => item !== id)
        }
      }
    
    }
    
    /**
     * Copyright Google LLC All Rights Reserved.
     *
     * Use of this source code is governed by an MIT-style license that can be
     * found in the LICENSE file at https://angular.io/license
     */
    

    component.html

    复选框

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 2019-12-08
      • 2021-10-07
      • 2020-04-26
      • 2020-05-07
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      相关资源
      最近更新 更多