AngularJS - How can I do a redirect with a full page load?
Asked Answered
E

5

94

I want to do a redirect that does a full page reload so that the cookies from my web server are refreshed when the page loads. window.location = "/#/Next" and window.location.href = "/#/Next" don't work, they do an Angular route which does not hit the server.

What is the correct way to make a full server request within an Angular controller?

Ezarra answered 14/4, 2013 at 18:51 Comment(9)
did you ever solve this?Centonze
did you ever solve this?Infold
did you ever solve this?Megasporophyll
did you ever solve this?Valoniah
did you ever solve this?Babyblueeyes
did you ever solve this?Underclassman
check this https://mcmap.net/q/223338/-angularjs-how-can-i-do-a-redirect-with-a-full-page-loadRollie
With plain JS this works in FF and Chrome: document.location.assign(<url>); location.reload(true);Sheba
did you ever solve this?Monologue
U
185

For <a> tags:

You need to stick target="_self" on your <a> tag

There are three cases where AngularJS will perform a full page reload:

  • Links that contain target element
    Example: <a href="/ext/link?a=b" target="_self">link</a>
  • Absolute links that go to a different domain
    Example: <a href="http://angularjs.org/">link</a>
  • Links starting with '/' that lead to a different base path when base is defined
    Example: <a href="/not-my-base/link">link</a>

Using javascript:

The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: $window.location.href.

See:

Uvular answered 14/4, 2013 at 18:54 Comment(16)
I'm doing this in a controller, not html.Ezarra
$window.location.href does the same thing that window.location.href does. I'm pretty sure $window is just a wrapping service for window. Either way both do an Angular route instead of the full page refresh.Ezarra
$window.location.href definitely causes a full page refresh, I'm doing it in my app.Uvular
From the docs on $location: "What does it not do? It does not cause a full page reload when the browser URL is changed. To reload the page after changing the URL, use the lower-level API, $window.location.href."Uvular
I've read the same thing. Doesn't work in the current version of Chrome. It does an Angular route.Ezarra
I'm using AngularJS 1.0.6 with the latest Chrome, it's working great. What version of AJS are you on?Uvular
1.0.6 from the CDN. I'm doing this within an $http.post success callback, if that matters, though I've tried it outside of the callback and gotten the same results. If I do the redirect to "/" (which I have an Angular route defined for as well), it works with the full page refresh, but "/#/Next" just does the view change.Ezarra
Might help then if you put together a JSfiddle for inspection, and update your question. I can't reproduce the issue.Uvular
have you tried location =location.href+ "/#next" ? or suitable variation that strips any hashesCannon
Same result. Angular route.Ezarra
how about meta no-cache tag? Seems very strange that angular is intercepting window.locationCannon
<a href="..." target="_self"> is what worked for me. thanks !Edgell
location.href alone does not work if it is same url with just different hash.Copernicus
To anyone stuck, the destinationUrl must contain the http:// prefix, or it will open ww.yourcurrenturl.com/destinationurlCustomer
i couldn't get any of them work, until i tried target _self option mentioned above.Transcurrent
Is there a way to do the analogous of <a href="/link" target="_self"> but in javascript? I tried to use window and $window but it always goes to an Angular route (I am using html5mode). I want to go to www.my-app-base-url/link sending the request to the server.Subalternate
S
33

We had the same issue, working from JS code (i.e. not from HTML anchor). This is how we solved that:

  1. If needed, virtually alter current URL through $location service. This might be useful if your destination is just a variation on the current URL, so that you can take advantage of $location helper methods. E.g. we ran $location.search(..., ...) to just change value of a querystring paramater.

  2. Build up the new destination URL, using current $location.url() if needed. In order to work, this new one had to include everything after schema, domain and port. So e.g. if you want to move to:

http://yourdomain.example/YourAppFolder/YourAngularApp/#/YourArea/YourAction?culture=en

then you should set URL as in:

var destinationUrl = '/YourAppFolder/YourAngularApp/#/YourArea/YourAction?culture=en';

(with the leading '/' as well).

  1. Assign new destination URL at low-level: $window.location.href = destinationUrl;

  2. Force reload, still at low-level: $window.location.reload();

Safford answered 13/11, 2013 at 10:58 Comment(2)
This doesn't work for me in Angular 1.3.1 with Chrome 38. The value of $window.location.href does not change when I assign a path or a full URL. Only $window.location.assign() worked for me.Shrift
Having the same problems as Felix, solved it in a different way #31138309Kaka
Z
13

After searching and giving hit and trial session I am able to solove it by first specifying url like

$window.location.href = '/#/home/stats';

then reload

$window.location.reload();
Zoosperm answered 1/7, 2016 at 10:19 Comment(1)
This seems to fix the issue, but clicking back and forward on the browser will cause a lot of inconsistencies.Shit
H
9

I had the same issue. When I use window.location, $window.location or even <a href="..." target="_self"> the route does not refresh the page. So the cached services are used which is not what I want in my app. I resolved it by adding window.location.reload() after window.location to force the page to reload after routing. This method seems to load the page twice though. Might be a dirty trick, but it does the work. This is how I have it now:

  $scope.openPage = function (pageName) {
      window.location = '#/html/pages/' + pageName;
      window.location.reload();
  };
Holder answered 7/6, 2015 at 9:39 Comment(0)
R
1

Try this

$window.location.href="#page-name";
$window.location.reload();
Rollie answered 22/6, 2018 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.