angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

URL out of sync with ng-view after a $routeChangeError

Open butchpeters opened this issue 13 years ago • 28 comments

  1. Configure a route (e.g. '/causesError') containing a resolve property
  2. Load a good state in your application (e.g. the route '/goodRoute')
  3. User performs an action to load the route '/causesError'
  4. The resolve gets rejected, which causes $routeChangeError to be broadcast rather than $routeChangeSuccess.

Because there was no $routeChangeSuccess, the ng-view still contains content for '/goodRoute', but now the URL is changed to the failed route. They are out of sync.

Any attempt to change the URL back to '/goodRoute' in a $routeChangeError event handler would cause the '/goodRoute' to reload, which is not desirable.

One problem this causes is now the user can't retry the action that caused the error. In a mobile application, for example, they may have been experiencing intermittent connection loss. If they retry the same action, the location change events won't fire because the URL isn't changing, and therefore the route won't be reattempted.

butchpeters avatar Mar 05 '13 14:03 butchpeters

Does anyone have a decent solution for this problem?

fozzle avatar Jul 22 '13 19:07 fozzle

It would be great if this could be fixed somewhere along the line...

xMysteriox avatar Dec 12 '13 08:12 xMysteriox

+1

marcalj avatar Dec 16 '13 20:12 marcalj

I have the same issue. My current solution is:

$rootScope.$on "$routeChangeError", (event, current, previous, rejection) ->
  $location.path(if not angular.isUndefined(previous) then previous.originalPath else "/")

There are two options here:

  1. If user goes to /causesError without a referrer, user will be redirected to / (home page).
  2. If user goes to /causesError from a link, user will be redirected to the last route (in this case: /goodRoute).

dogancelik avatar Jan 20 '14 08:01 dogancelik

Just a note: the above code won't work for routes with parameters, originalPath would contain the literal /goodRoute/:param

austingreco avatar Jan 21 '14 22:01 austingreco

Oh I didn't notice that, thank you. Here's a little fix, I don't know how to do this the Angular way:

previous.originalPath.replace(/:(\w+)/g, (match, param) -> previous.pathParams[param])

dogancelik avatar Jan 22 '14 01:01 dogancelik

+1 need a clean solution too

bbshopadmin avatar Jan 28 '14 11:01 bbshopadmin

In case of rejected resolve on route change: there is a needless entry in the history and a wrong location is shown. Any ideas?

bbshopadmin avatar Jan 28 '14 12:01 bbshopadmin

I can't believe not finding a solution for that issue. In my case i don't want to redirect to error pages, so i want to stay in the current view.

Is route resolve simply unusable? What i'm overlooking?

bbshopadmin avatar Jan 28 '14 21:01 bbshopadmin

What about to show route defined as otherwise case? It is intended to be shown when other routes can't do it. I think, that is good solution, because usually there is described a fallback page for wrong route. It will be much better than loading previous path when I have to listen $routeChangeError and show error message manually.

just-boris avatar Feb 06 '14 14:02 just-boris

For now i redirect to an error view, though i'd like to handle route change errors messages via popup or something like that.

bbshopadmin avatar Feb 06 '14 14:02 bbshopadmin

The main reason, why I don't use pop-ups for error, it that is when opening page with error by direct link you will see the only small block instead of full-page message. For example, my app has route '/project/:id' and if I request non-existing id and server returns an error I want to show 404 page for user, and this is common use case of my routes. Could you provide your example where you need your behaviour?

P.S. Popups is only useful when you have a deal with request without route change, e.g. form submit or pagination. In this case current view may has an unsaved user data, which cannot be lost.

just-boris avatar Feb 06 '14 15:02 just-boris

@just-boris I use angular-http-auth and $route resolve. I tried to explain my issue here: https://github.com/witoldsz/angular-http-auth/issues/55 .

bbshopadmin avatar Feb 06 '14 17:02 bbshopadmin

Very interesting. +1 for the route reload usage. With authorization I see the following steps:

  1. User clicks on link, requiring authorization.
  2. Server respond 401 error
  3. We capture this error and show auth form.
  4. There two ways:
    • User successfully logged in: call $route.reload().
    • User clicks cancel: call $window.history.back(); to get previous page.

My scheme ideally covers this case

just-boris avatar Feb 06 '14 18:02 just-boris

:+1:

marcghorayeb avatar Mar 04 '14 17:03 marcghorayeb

+1

ovax3 avatar Mar 05 '14 09:03 ovax3

Any update on this?

sinelaw avatar Oct 26 '14 10:10 sinelaw

severity: inconvenient !! is there a proper way to not having a wrong route in history, though resolve fails before route change?

bsr203 avatar Jan 06 '15 15:01 bsr203

:+1:

veritas1 avatar Jan 30 '15 11:01 veritas1

Building on @just-boris and @dogancelik comments, I'm using this code and it works good:

$rootScope.$on("$routeChangeError", function(event, current, previous, rejection) {
  if (previous) {
    $window.history.back();
  } else {
    $location.path("/").replace();
  }
});

(assumes "/" is your default route)

amitport avatar Feb 15 '15 19:02 amitport