在离子切换中切换开关时如何调用不同的功能

Adi*_*tya 0 typescript ionic-framework ion-toggle ionic2 ionic3

当切换按钮"打开"时,请参阅下面的函数buttonOn(),
PIC1

我想在buttonOff()"关闭"时切换它,如下所示

在此输入图像描述

html的

<ion-toggle (ionChange)="buttonOn()"></ion-toggle>
Run Code Online (Sandbox Code Playgroud)

.TS

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

buttonOff() {
   // how to call this function when toggle button gets off?
}
Run Code Online (Sandbox Code Playgroud)

小智 5

让我们ngModel用来绑定你的toggle的状态,然后调用函数取决于状态

html的

<ion-toggle [(ngModel)]="status" (ionChange)="onChange()"></ion-toggle>
Run Code Online (Sandbox Code Playgroud)

.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?
}
Run Code Online (Sandbox Code Playgroud)