Angularjs - Remove last history entry after $routeChangeError

3.4k views Asked by At

I have a route which has to resolve a server side resource. That resource asks for authentication. So i show a login form popup which is cancelable. On cancel the route/resolve gets rejected and $routeChangeError fires correctly.

The issue is, that i now have that failed location/url in address bar and a needless history entry.

How do you replace the current location url to the last one without reload? And how do you remove the current history entry after route change failed?

Edit: I use angular-http-auth. Here is my routing:

/home > /item (has to resolve a resource that requires authentication, when not logged in a login form popup opens up which is cancelable)

When i cancel the authentication a $routeChangeError gets fired and i'm still on /home page due to /item never rendered. Now the address bar shows a wrong url: /item

1

There are 1 answers

9
Ben Lesh On

You just need to send them back one step in your history. Don't worry about the history record being there, it will be overwritten when they navigate to the next thing:

app.run(function($rootScope, $window) {
   $rootScope.$on('$routeChangeError', function () {
      $window.history.back();
   });
});

And here's a demo Plunk