4

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

Ben Lesh
  • 107,825
  • 47
  • 247
  • 232
tagomago
  • 99
  • 2
  • 7

1 Answers1

2

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

Ben Lesh
  • 107,825
  • 47
  • 247
  • 232
  • Thanks, but that reloads the page i'm already/still seeing.I use angular-http-auth and open a login popup when a resource on a route (with resolve) requires authentication. When i cancel that login popup i see the right page, but the wrong location. With $location.replace() i'm able to get rid of that needless history entry on next route change. But that's no solution. I have to change the $location on $routeChangeError without reloading the same page. – tagomago Jan 29 '14 at 13:46
  • You need to provide more code then. I thought I had an idea of what you needed to do, but right now I have no idea what you're trying. Also, I'd recommend tagging your question with "angular-http-auth" or whatever you're using and perhaps sending a link to the author of the module. – Ben Lesh Jan 29 '14 at 14:12
  • I'm unfortunately not allowed to open a new tag "angular-http-auth". – tagomago Jan 29 '14 at 15:49
  • I think i can reduce my question to: how to revert the current location url to the previous one without reloading that page. A page reload isn't useful when you're already on that 'previous' page. My route change simply failed and i don't want to redirect to an error page and replace() or something like that. – tagomago Jan 29 '14 at 15:58
  • Normally when you have a rejected route change you'll probably show an error page. But my login form is popup based and cancelable. There is no point in redirecting to error pages. – tagomago Jan 29 '14 at 16:10
  • I've added the tag for you. – Ben Lesh Jan 29 '14 at 17:07
  • I haven't seen a way to do what you're trying to do though. Which seems to be something like "load a previous route without actually loading the previous route". – Ben Lesh Jan 29 '14 at 17:09
  • Perhaps your best bet would be to try to load the data in the controller you're in, then persist it to the next route, so you can avoid trying to switch to begin with? Not sure... Either way... best of luck to you. – Ben Lesh Jan 29 '14 at 17:10
  • 1
    You're right. I should use a service again, like i already did on another project. That route/resolve thing is intresting but seems limited or almost questionable. Pity! Maybe ui-router is more flexible.Thank you very much! – tagomago Jan 29 '14 at 22:47