'How can i check all mat-checkbox in angular5?

I want when I click check-all; check-1, check-2 and check-3 are also checked. How can I do that?

<form>
    <mat-checkbox type="checkbox" class="select_all">
        Check all
    </mat-checkbox>
    <br>
    <mat-checkboxtype="checkbox" class="select_single">
        Check-1
    </mat-checkbox>
    <mat-checkbox type="checkbox" class="select_single">
        Check-2
    </mat-checkbox>
    <mat-checkbox type="checkbox" class="select_single">
        Check-3
    </mat-checkbox>
</form>


Solution 1:[1]

You can do something like this. in html

<table class="table">
   <thead class="green-color">
      <tr>
         <th>
            <input type="checkbox" (change)="checkAll($event)">
         </th>
      </tr>
   </thead>
   <tbody>
      <tr *ngFor="let list of orders">
         <td>
            <input type="checkbox" value="{{list.number}}" [(ngModel)]="list.isChecked">
         </td>
      </tr>
   </tbody>
</table>

and in your .ts file

checkAll(event) {
  this.orders.forEach(object => object.isChecked = event.target.checked);
 }

Solution 2:[2]

You can make use of change event of mat-checkbox and [checked] input property to achieve check all functionality as below:

HTML:

<mat-checkbox (change)="chkAllChange($event)" type="checkbox" class="select_all">Check all</mat-checkbox>
<br/>
<mat-checkbox type="checkbox" class="select_single" [checked]="chkArr[0]">Check-1</mat-checkbox>
<mat-checkbox type="checkbox" class="select_single" [checked]="chkArr[1]">Check-2</mat-checkbox>
<mat-checkbox type="checkbox" class="select_single" [checked]="chkArr[2]">Check-3</mat-checkbox>

TS:

chkArr:boolean[]=[false,false,false];

  chkAllChange(event:MatCheckboxChange){
    if(event.checked){
      this.chkArr = this.chkArr.map(m=>true);
    }
    else{
      this.chkArr = this.chkArr.map(m=>false);
    }
  }

Demo here: https://stackblitz.com/edit/angular-ngjvt8

Solution 3:[3]

Please check the html and ts snippets below to implement select all or deselect all with mat-checkbox

.html file

     <form>
    <ul>
        <mat-checkbox type="checkbox" class="select_all" (change)="handleAllSelections($event)">
            Check all
        </mat-checkbox>
        <br>
        <div *ngFor="let checkbox of checkBoxList">
            <mat-checkbox type="checkbox" class="select_single" [checked]="checkbox.selected">
                {{checkbox.name}}
            </mat-checkbox>
        </div>
    </ul>
</form>

.ts file

Define checkBoxList to avoid the repeated lines in html code and add 'handleAllSelections' function to select or deselect all checkboxes.

  checkBoxList = [
        {
          name: 'Check-1',
          selected: false
        },
        {
          name: 'Check-2',
          selected: false
        },
        {
          name: ' Check-3',
          selected: false
        }
      ];


 handleAllSelections(event: MatCheckboxChange) {
    if (event.checked) {
      this.checkBoxList.forEach(element => {
        element.selected = true;
      });
    } else {
      this.checkBoxList.forEach(element => {
        element.selected = false;
      });
    }
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimantha
Solution 2 Nimantha
Solution 3 sajiyya k