How to check is checkbox is checked angular2

前端 未结 2 1733
清酒与你
清酒与你 2020-12-18 09:04

I want to check is checkbox is checked or unchecked in an if and else statement.

Ch         


        
相关标签:
2条回答
  • 2020-12-18 09:11

    declare a variable called it filter

    filter: boolean= false;
    

    then use ngModel in html part to access and assign its value.

    <input type="checkbox" [(ngModel)]="filter" (click)="filterData()">
    

    it will call a function filterData() which you can use to do all your functionality

    filter(){
      this.filter = !this.filter;// this will change value of it true and false 
    }
    

    for more checkbox you can use declare more variable like filter1: boolean

    0 讨论(0)
  • 2020-12-18 09:13

    We can look at source code of material checkbox:

    event.source = this;
    event.checked = this.checked;
    
    this._controlValueAccessorChangeFn(this.checked);
    this.change.emit(event);
    

    https://github.com/angular/material2/blob/2.0.0-alpha.11/src/lib/checkbox/checkbox.ts#L288-L292

    So you can do it like:

    mon(e){
      if(e.checked){
        console.log("This is checked")
      } else {
        console.log("unchecked");
      }
    }
    
    0 讨论(0)
提交回复
热议问题