Skip to content

Instantly share code, notes, and snippets.

@bartoszbobin
Last active September 20, 2016 14:33
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 bartoszbobin/5d5d922da535fed3aa3b4565971203ce to your computer and use it in GitHub Desktop.
Save bartoszbobin/5d5d922da535fed3aa3b4565971203ce to your computer and use it in GitHub Desktop.
UI Router History Service
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