1

I am looking for the best way to not screw up the back functionality of the browser if a resolve for an angular route fails. Right now I am doing the following:

$location.path(lastUrls[0]);
$location.replace();
window.history.back();

While this works I would like to know if there might be a better solution available. If my url history looks like this:

www.example.com -> www.example.com/items -> www.example.com/items/123

and if item 123 has a resolve for that item and it returns a 404 error then my browser history looks like this:

www.example.com -> www.example.com/items -> www.example.com/items

however because I have the window.history.back(); call, the browser is at the 2nd to last item in the history, not the last one. That back() call is in there so if the user does hit the back button, they are going to be taken to the page they expect where if I didn't have the call to back(), it would just bring them back to the page they are current on because now there are two of them in the history.

Is the way I am doing this the best possible way or is there a better way within angular?

ryanzec
  • 27,284
  • 38
  • 112
  • 169
  • I understand what you're doing but I am not sure if it's a good idea of relying on the browser current location to handle route failures. Wouldn't be better to use the event on `$routeChangeError` if a resolve fails and have a callback?! You could even catch the type of error and react differently for each type. Like this you would have much control of your app state. Just an idea/suggestion :) - see here something I did along these lines that might help you (http://stackoverflow.com/q/18661313/1310945) – Denison Luz Sep 11 '13 at 20:20
  • That $location code is inside the error event. – ryanzec Sep 11 '13 at 20:26

0 Answers0