registerBackButtonAction

registerBackButtonAction

我是离子新手。我正在使用ionic的onHardwareBackButton事件,该事件正在正常运行,这使我需要注册功能,但是在要注册功能后,它仍导航至后页。
我正在向cordova确认硬件backbutton事件功能上的dilogbox确认,因此在单击cancle后,他可以导航到后一页,但是现在弹出窗口也来了,页面也同时在向后导航。我已经搜索并尝试了许多代码,例如

e.preventDefault()

e.stopPropagation()


他们俩都不工作
我也尝试了registerBackButtonAction事件,但是当我离开页面时并没有取消注册。
我从很多小时就一直困扰着这个问题。
我正在使用的代码如下。

//this is register event i have used

showalertPopup = function(){
//showing popup
}

$scope.$on '$ionicView.enter', (event, view)->
    $ionicPlatform.registerBackButtonAction showalertPopup, 100

//like this i am diregistering event
$scope.$on '$ionicView.leave', (event, view)->
    $ionicPlatform.offHardwareBackButton showalertPopup


我已经使用onHardwareBackButton代替registerBackButtonAction

最佳答案

您可以在此处执行优先级为100的registerBackButtonAction(请参阅docs):


  现有后退按钮挂钩的优先级如下:
  返回上一个视图= 100
  侧面菜单= 150
  消除模态= 200
  结束动作表= 300
  关闭弹出窗口= 400
  取消加载叠加= 500


因此,基本上,您将覆盖“返回上一个视图”操作。
离开视图时,您将需要一个处理程序来注销:

var backbuttonRegistration = null;

$scope.$on('$ionicView.enter', function(event, viewData) {
    backbuttonRegistration = $ionicPlatform.registerBackButtonAction(function(e) {
            e.preventDefault();
            showalertPopup();
        }, 100);
});

$scope.$on('$ionicView.leave', function(event, viewData) {
    if (backbuttonRegistration)
    {
        backbuttonRegistration();
    }
});


根据文档registerBackButtonAction


  返回:一个函数,该函数在调用时将注销此函数
  backButtonAction。


您的控制器应如下所示:

  .controller('homeController', function($scope, $ionicPopup, $ionicPlatform) {

    function showalertPopup() {
      var alertPopup = $ionicPopup.alert({
        title: 'Don\'t eat that!',
        template: 'It might taste good'
      });
      alertPopup.then(function(res) {
        console.log('Thank you for not eating my delicious ice cream cone');
      });
    }

    var backbuttonRegistration = null;

    $scope.$on('$ionicView.enter', function(event, viewData) {
        backbuttonRegistration = $ionicPlatform.registerBackButtonAction(function(e) {
                e.preventDefault();
                showalertPopup();
            }, 100);
    });

    $scope.$on('$ionicView.leave', function(event, viewData) {
        if (backbuttonRegistration)
        {
            backbuttonRegistration();
        }
    });

});


PS:

您可以将registerBackButtonAction设置为最高优先级-假设为1000-它将起作用:

    backbuttonRegistration = $ionicPlatform.registerBackButtonAction(function(e) {
            e.preventDefault();
            showalertPopup();
        }, 1000);

09-25 19:13