回到离子框架中时触发动作

时间:2016-05-01 10:36:42

标签: angularjs ionic-framework

使用离子框架在移动设备中有两种返回先前状态的方法,即通过ion-nav-back-button和设备后退按钮本身。

当我点击ion-nav-back-button并按设备返回按钮返回上一状态之前需要触发操作时,我应该调用什么功能?

2 个答案:

答案 0 :(得分:0)

我认为以下内容应该提供您正在寻找的解决方案

唯一的问题是,我不确定这个解决方案是否可以100%用于所有设备上的硬件后退按钮....

                // run this function when either hard or soft back button is pressed
            var doCustomBack = function() {
            // your custom handler code here
            .....
                //});

                $ionicHistory.goBack();
            };

            // override soft back
            // framework calls $rootScope.$ionicGoBack when soft back button is pressed
            var oldSoftBack = $rootScope.$ionicGoBack;
            $rootScope.$ionicGoBack = function() {
                doCustomBack();
            };
            var deregisterSoftBack = function() {
                $rootScope.$ionicGoBack = oldSoftBack;
            };

            // override hard back
            // registerBackButtonAction() returns a function which can be used to deregister it
            var deregisterHardBack = $ionicPlatform.registerBackButtonAction(
                doCustomBack, 101
            );

            // cancel custom back behaviour
            $scope.$on('$destroy', function() {
                deregisterHardBack();
                deregisterSoftBack();
            });

答案 1 :(得分:0)

试试这个

 <ion-nav-back-button class="button-clear"
    ng-click="myGoBack()">
    <i class="ion-arrow-left-c"></i> Back
  </ion-nav-back-button>

function MyCtrl($scope, $ionicHistory) {
  $scope.myGoBack = function() {
    $ionicHistory.goBack();
  };
}