Search code examples
angularjsangular-ui-routerangularjs-routing

Angular - ui-router get previous state


Is there a way to get the previous state of the current state?

For example I would like to know what the previous state was before current state B (where previous state would have been state A).

I am not able to find it in ui-router github doc pages.


Solution

  • ui-router doesn't track the previous state once it transitions, but the event $stateChangeSuccess is broadcast on the $rootScope when the state changes.

    You should be able to catch the prior state from that event (from is the state you're leaving):

    $rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
       //assign the "from" parameter to something
    });