Last active
September 20, 2016 14:33
-
-
Save bartoszbobin/5d5d922da535fed3aa3b4565971203ce to your computer and use it in GitHub Desktop.
UI Router History Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import IStateService = angular.ui.IStateService; | |
import IRootScopeService = angular.IRootScopeService; | |
import IState = angular.ui.IState; | |
class RouterHistory | |
{ | |
private history : IHistoryEntry[] = []; | |
public add(stateName : string, stateParams : any) : void | |
{ | |
this.history.unshift({ | |
state: stateName, | |
params: stateParams | |
}); | |
if (this.history.length > 10) { | |
this.history.splice(10); | |
} | |
} | |
public getPrevious() : IHistoryEntry | |
{ | |
if (this.history.length === 0) { | |
return null; | |
} | |
return this.history[0]; | |
} | |
} | |
interface IHistoryEntry | |
{ | |
state: string; | |
params: any; | |
} | |
angular.module("bobin.router", []) | |
.service("RouterHistory", RouterHistory) | |
.run(["RouterHistory", "$state", "$rootScope", (RouterHistory : RouterHistory, $state : IStateService, $rootScope : IRootScopeService) => { | |
$rootScope.$on("$stateChangeSuccess", (event : any, to : IState, toParams : any, from : IState, fromParams : any) => { | |
if (!from.abstract) { | |
RouterHistory.add(from.name, fromParams); | |
} | |
}); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment