Override ionic application back button in the controller with example code

Ionic application back button can be easily override using the following example, The default behaviour of the ionic application back button is the history back to one level that is,

$ionicHistory.goBack();

Below code override the default behaviour of the back button for a specific condition mentioned at “if statement”, the  else part handles with the normal behaviour

$rootScope.$ionicGoBack = function () {
      if($location.path() == 'condition') {
          $location.path('ourCustomUrl');
      } else {
          $ionicHistory.goBack();
      }
};

 

Category: