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

时间:2018-04-07 10:20:11

标签: typescript ionic-framework ionic2 ionic3 ion-toggle

当切换按钮处于“打开”状态时,请参阅下面的函数buttonOn()                                  pic1

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

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?
}

1 个答案:

答案 0 :(得分:5)

让我们用<ion-toggle [(ngModel)]="status" (ionChange)="onChange()"></ion-toggle> 来绑定你的toggle的状态,然后调用函数取决于状态

<强> html的

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?
}

<强> .TS

<element>
    <subelment1>text</subelement1>
    <subelement2>text2</subelement2>
    ...
</element>