AngularJS-如何做一个重定向与一个完整的页面加载?

我想做一个重定向,做一个完整的页面重新加载,以便从我的网络服务器的 Cookie 刷新时,页面加载。window.location = "/#/Next"window.location.href = "/#/Next"不工作,他们做一个角路线,不打服务器。

在 Angular 控制器中发出完整服务器请求的正确方法是什么?

153147 次浏览

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:

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();

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();
};

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();

Try this

$window.location.href="#page-name";
$window.location.reload();