I want to be able to override the BACK button on the navigation bar, and the hardware button.
I want this override to be for one specific controller, but not for the
I took Richard's suggestion and put it into a service to make it more reusable.
The Controller
angular.module('MainApp').controller('MyController', ['backButtonOverride'], function (backButtonOverride) {
// override back button for this controller
backButtonOverride.setup($scope, function() {
console.log("custom back");
});
}
The Service
angular.module('MainApp.services', []).factory('backButtonOverride', function ($rootScope, $ionicPlatform) {
var results = {};
function _setup($scope, customBackFunction) {
// override soft back
// framework calls $rootScope.$ionicGoBack when soft back button is pressed
var oldSoftBack = $rootScope.$ionicGoBack;
$rootScope.$ionicGoBack = function() {
customBackFunction();
};
var deregisterSoftBack = function() {
$rootScope.$ionicGoBack = oldSoftBack;
};
// override hard back
// registerBackButtonAction() returns a function which can be used to deregister it
var deregisterHardBack = $ionicPlatform.registerBackButtonAction(
customBackFunction, 101
);
// cancel custom back behaviour
$scope.$on('$destroy', function() {
deregisterHardBack();
deregisterSoftBack();
});
}
results.setup = _setup;
return results;
});