//Adapted from http://stackoverflow.com/a/36733061/114558
export class BackButtonOverrideService {
static $inject = ["$rootScope", "$ionicPlatform"];
constructor(private $rootScope: ng.IRootScopeService, private $ionicPlatform: ionic.platform.IonicPlatformService) {
}
setup($scope: ng.IScope, customBackFunction: Function) {
var that = this;
//Override soft back.
var originalSoftBack = (<any> this.$rootScope).$ionicGoBack;
(<any> this.$rootScope).$ionicGoBack = function () {
customBackFunction();
};
//Override hard back.
// registerBackButtonAction() returns a function which can be used to deregister it
var restoreHardBack = this.$ionicPlatform.registerBackButtonAction(
customBackFunction, 101
);
//Restore original back button behaviour
$scope.$on("$destroy", function() {
//Restore hard back.
restoreHardBack();
//Restore soft back.
// framework calls $rootScope.$ionicGoBack when soft back button is pressed
(<any> that.$rootScope).$ionicGoBack = originalSoftBack;
});
}
}
angular.module("app.services").service("backButtonOverride", BackButtonOverrideService);