簡體   English   中英

Angular 4選中所有復選框

[英]Angular 4 select all checkboxes

我正在嘗試實現全選復選框,這將選擇所有復選框,但是當我單擊除全選以外的其他復選框時,它將再次選擇所有復選框

HTML文件

<div class="form-check mt-3">
  <label class="form-check-label text-dark font-weight-bold">
                <input class="form-check-input" type="checkbox" value="" [(ngModel)]="selectedAll" [ngModelOptions]="{standalone:
       true}" (change)="selectAll()">
                Select All
                <span class="form-check-sign">
                <span class="check"> </span>
              </span>
              </label>
</div>

<div *ngFor="let a of name">

  <div class="d-flex justify-content-between">

    <!--------------------div to justify content --------------------->
    <div class="">

      <div class="form-check mt-3">
        <label class="form-check-label text-dark">
                    <input class="form-check-input" type="checkbox" value="" [(ngModel)]="a.selected" [ngModelOptions]="{standalone:
       true}" (change)="checkIfAllSelected()">
                    Household Information
                    <span class="form-check-sign">
                <span class="check"> </span>
              </span>
                  </label>
      </div>

      <div class="form-check mt-3">
        <label class="form-check-label text-dark">
                    <input class="form-check-input" type="checkbox" value="" [(ngModel)]="a.selected" [ngModelOptions]="{standalone:
       true}" (change)="checkIfAllSelected()">
                    House Images
                    <span class="form-check-sign">
                <span class="check"> </span>
              </span>
                  </label>
      </div>

      <div class="form-check mt-3">
        <label class="form-check-label text-dark">
                    <input class="form-check-input" type="checkbox" value="" [(ngModel)]="a.selected" [ngModelOptions]="{standalone:
       true}" (change)="checkIfAllSelected()">
                    Map Access
                    <span class="form-check-sign">
                <span class="check"> </span>
              </span>
                  </label>
      </div>
    </div>

ts文件

selectAll() {
  for (let i = 0; i < this.name.length; i++) {
    {
      this.name[i].selected = this.selectedAll;
    }
  }
}

checkIfAllSelected() {

  console.log(this.name);

  this.selectedAll = this.name.every(function(item: any) {
    console.log(item);
    return item.selected == true;
  })

}

刪除[ngModelOptions]="{standalone: true}" ,它應該可以工作。 有關更多信息,請參考鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM