Aditya
Aditya

Reputation: 2536

How to call different function when toggling on and off in ion-toggle

When Toggle button is "on" see below the function buttonOn() is called,
pic1

I want to call buttonOff() when toggling it "off" as below

enter image description here

.html

<ion-toggle (ionChange)="buttonOn()"></ion-toggle>

.ts

buttonOn() { 
    // this function is called;
}

buttonOff() {
   // how to call this function when toggle button gets off?
}

Upvotes: 0

Views: 1693

Answers (1)

Hnampk
Hnampk

Reputation: 517

Let's use ngModel to bind your toggle's status, then call the function depends on the status

.html

<ion-toggle [(ngModel)]="status" (ionChange)="onChange()"></ion-toggle>

.ts

status=true;
onChange(){
  if(this.status){
    this.buttonOn();
  }
  else{
    this.buttonOff()
  }
}

buttonOn() { 
    // this function is called;
}

buttonOff() {
   // how to call this function when toggle button gets off?
}

Upvotes: 5

Related Questions