Skip to content

Instantly share code, notes, and snippets.

@rinogo
Created December 21, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rinogo/1bd363c1b1ce19ef59477b55ea63b474 to your computer and use it in GitHub Desktop.
Save rinogo/1bd363c1b1ce19ef59477b55ea63b474 to your computer and use it in GitHub Desktop.
Ionic 1 (Angular 1.x) back button override - Typescript version
//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);
...
backButtonOverride.setup($scope, function() {
console.log("custom BACK");
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment