How to check the box marked angular2

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

<md-checkbox id="mon" (change)="mon($event)" [checked]="true">Checked</md-checkbox>



mon(e){
    if(e.target.checked){
      console.log("This is checked")
    } else {
      console.log("unchecked");
    }
  }

I think I'm doing it wrong. I keep getting, can't get undefined check. How to do it?

+4
source share
2 answers

We can see the source code of the material:

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");
  }
}
+3
source

declare a variable called filter

filter: boolean= false;

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

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

it is called by the filterData () function, which you can use to perform all your functions.

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

declare more variable filter1: boolean

+1

Source: https://habr.com/ru/post/1664298/


All Articles