Call custom function on click of ionic 2 RC.1 Back button

时间:2016-10-20 18:51:11

标签: ionic2

How can I call a function on click of back button in Ionic 2 from a specific page?

I have the following scenario in my mind. Lets say I navigate like below:

PageA --> PageB --> PageC --> PageD

Now when I click back button on PageD I want to go back to PageB instead of PageC. I want to call below function on click of back button.

goBack(){
    this.navCtrl.popTo(PageB);
}

I have done it Like this:

ionViewDidEnter(){
    this.bindMethodToElement('back-button',this.goBack);
}

bindMethodToElement(elementClassName,functionToBind){
    try{
      let elements = document.getElementsByClassName(elementClassName);
      let currentElement : Element = elements[elements.length - 1];
      currentElement.addEventListener("click",functionToBind);
    }catch(exception){
      console.log(exception.message);
      throw exception;
    }
  }

Does anyone know better approach to do it. I also want to achieve similar behavior when user clicks on the device back button.

1 个答案:

答案 0 :(得分:1)

此类功能已经向Ionic团队报告,他们正在验证它。在此期间,您可以使用此解决方法:

goBack(){
  this.navCtrl.pop().then(() => {this.navCtrl.pop()});
}

丑陋的解决方案但有效。祝你好运!