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?

Mike Pateras
  • 14,715
  • 30
  • 97
  • 137

5 Answers5

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:

MetaGuru
  • 42,847
  • 67
  • 188
  • 294
jszobody
  • 28,495
  • 6
  • 61
  • 72
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();

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
superjos
  • 12,189
  • 6
  • 89
  • 134
  • 1
    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. – FelixM Nov 19 '14 at 14:09
  • Having the same problems as Felix, solved it in a different way http://stackoverflow.com/questions/31137809/setting-window-location-href-not-working-as-expected – Soroush Hakami Jun 30 '15 at 12:51
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();
Daniyal
  • 690
  • 6
  • 10
  • This seems to fix the issue, but clicking back and forward on the browser will cause a lot of inconsistencies. – luisluix Apr 12 '17 at 20:17
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();
  };
Neel
  • 9,352
  • 23
  • 87
  • 128
1

Try this

$window.location.href="#page-name";
$window.location.reload();
imskm
  • 780
  • 1
  • 14
  • 27