如何在离子中启用设备后退按钮?

时间:2017-10-24 10:22:35

标签: angularjs ionic-framework back-button

我已经通过这样的backbutton注册操作事件禁用某些条件的后退按钮:

    $ionicPlatform.registerBackButtonAction(function (event) {
    if (condition)
       {
       event.preventDefault();
       $ionicHistory.nextViewOptions({ disableBack: true });
       } 
    else
       {
       $ionicHistory.goBack();
       }
       }, 800);

那么我现在如何再次启用该设备后退按钮? 因为它仍然被禁用,也没有进入以前的视图。

1 个答案:

答案 0 :(得分:0)

你需要试试这个

var lastTimeBackPress = 0;
  var timePeriodToExit = 2000;

  platform.registerBackButtonAction(() => {
    // get current active page
    let view = this.nav.getActive();
    if (view.component.name == "HomePage") {
      //Double check to exit app                  
      if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
        platform.exitApp(); //Exit from app
      } else {
        let toast = this.toastCtrl.create({
          message: 'Press back again to exit App',
          duration: 3000,
          position: 'bottom'
        });
        toast.present();
        lastTimeBackPress = new Date().getTime();
      }
    } else {
      // go to previous page
      this.nav.pop({});
    }
  });

希望它适合你