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

Angular $route.reload does not reload the current template

Open parliament718 opened this issue 12 years ago • 6 comments

When a use visits a private page unauthorized, say profile, my backend 302 redirects to a controller action that serves up the login partial in place of the profile partial. Since it 302 redirects to an action that returns a partial, the url address bar doesn't change from the page the user was trying to access ("/profile").

I was going to "fix" that but actually I think it makes a good user experience instead of dealing with return urls as query params.

The idea is once they log in I just want to reload the current route aka do a GET request for the profile partial via "/profile" and switch it back in instead of the login partial.

However, I can't get this "reload current route" to work. I tried all of the following:

$location.$$compose();
$location.absUrl($location.path());
$location.url($location.path());
$location.path($location.path())
$route.reload(); 

But none work. $route.reload() seems to be the definite way but it also doesn't work. It goes through the route cycle, reinstantiates the controller, but does not do GET request to reload the template

The only thing that works is a hard refresh via location.reload() but that is not ideal.

How can I force angular to reload the template for the current route?

parliament718 avatar Feb 16 '14 21:02 parliament718

I am not exactly sure of the issue you are seeing, maybe some clarification is needed, but from my understanding this can be split into two parts

  1. Using the retrieval of a templates to know if a user has the right permissions to get to a page, can be problematic. Templates are cached, if a user does not have the permissions to be in a page, then this check should be done before it reaches the controller or after, within the controller, but not on the template retrieval
  2. If this is the way you want to follow, then you need to remove the template from the $templateCache before you call reload

lgalfaso avatar Feb 17 '14 09:02 lgalfaso

You understood correctly and your solution works, thanks a lot.

This is exactly what I want to do because the login template actually gets cached as the template the user was trying to access. So removing it and letting angular re-fetch the correct one for the current route worked like a charm.

var currentPageTemplate = $route.current.templateUrl;
$templateCache.remove(currentPageTemplate);
$route.reload();

parliament718 avatar Feb 18 '14 17:02 parliament718

Its awesome post! Its might helpful $route.reload();

anilsingh581 avatar May 19 '15 08:05 anilsingh581

var currentPageTemplate = $route.current.templateUrl;
$templateCache.remove(currentPageTemplate);
$route.reload();

when I use the solution, I can not pass in another route, because it always reload when I click the link. Do you guys have another solution please!!

minhduc19 avatar Mar 30 '16 07:03 minhduc19

@minhduc19, this sounds like a general support question. Please, use one of the appropriate support channels for these types of questions. GitHub issues are reserved for bug reports and feature requests.

Thx !

gkalpak avatar Mar 30 '16 07:03 gkalpak

It's not working. Still is getting from cache

var currentPageTemplate = $route.current.templateUrl; $templateCache.remove(currentPageTemplate); $route.reload();

sudhirkumargpt avatar Nov 18 '17 10:11 sudhirkumargpt