Previous to page with ui-router

I have a SPA system and to do the routes I am using the ui-router $stateProvider.state and this case the browser not save the history to that I can return to last page, I tried many things and didn’t work yet, in this case I need that when the user clicked in Cancel button he return to last page, How I do this?

Answer

You can add an event on stateChangeSuccess to keep in memory your last state:

$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
   /* Here, from is the previous page you want to recover */
   /* For example: */
   $state.previousState = from;
});

You will be able to use it in $state.go:

$state.go($state.previousState.name);

Note: using stateChangeStart may work as well.

Leave a Reply

Your email address will not be published. Required fields are marked *