Is it possible to override the back button behavior on certain screens?

Is there an easy way to override the back button behavior on certain screens? I’ve been using leftButtons, but was hoping there was an easier way so I don’t need to put this in every controller that I want to change the behavior for.

The reason I want to change the behavior is because I’m integrating my app in a native iOS app and sometimes we want to go “back” into the app, using a custom protocol. However, when testing it outside of the app, I want the normal ionic back behavior.

        $scope.leftButtons = [
        {
            type: 'button-clear',
            content: '<i class="icon ion-arrow-left-c"></i>',
            tap: function (e) {
                if ($rootScope.isNative) {
                    location.href='jmh-ios://back'
                } else {
                    // this doesn't always work as expected
                    history.back();
                }
            }
        }
    ];

Perhaps try using state.go('xxxxx') instead?

I don’t know if you have solved the problem, but it’s possible.

Example below:

  1. add ng-click to the back button:
<ion-nav-bar class="bar-stable nav-title-slide-ios7">
  <ion-nav-back-button class="button-clear" ng-click="askSave()"><i class="icon ion-ios7-arrow-back"></i>Back</ion-nav-back-button>
</ion-nav-bar>
  1. Use $ionicNavBarDelegate to control the navigation:
$rootScope.askSave = function() {
  if($rootScope.isNative){
    location.href='jmh-ios://back';
  }else{
    $ionicNavBarDelegate.back();
  }
}
2 Likes