AngularJS -如何重定向与全页负载?

时间:2022-05-21 07:19:04

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.

我想进行重定向,该重定向执行全页重载,以便在页面加载时刷新web服务器上的cookie。窗口。位置= "/#/Next"和window.location。href = "/#/Next"不工作,他们做一个角度路径不会击中服务器。

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

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

5 个解决方案

#1


179  

For <a> tags:

<一> 标记:

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

您需要在标记上粘贴target="_self"

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

有三种情况下,AngularJS将执行全页重载:

  • 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>
  • 以'/'开头的链接,当base被定义时,会导致不同的基路径。例如:link

Using javascript:

使用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.

$location服务允许您只更改URL;它不允许重载页面。当您需要更改URL并重新加载页面或导航到另一个页面时,请使用较低级别的API: $window.location.href。

See:

看到的:

#2


32  

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

我们也遇到了同样的问题,使用JS代码(即不使用HTML锚)。我们就是这样解决的:

  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.

    如果需要,可以通过$location服务修改当前URL。如果您的目的地只是当前URL的一个变体,那么这可能很有用,以便您可以利用$location helper方法。例如我们跑location.search美元(…只是更改querystring参数的值。

  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:

    如果需要,使用当前的$location.url()构建新的目标URL。为了工作,这个新的必须包括所有的东西,包括模式,域和端口。例如,如果你想:

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

    http://yourdomain.com/YourAppFolder/YourAngularApp/ / YourArea / YourAction ?文化= en

    then you should set URL as in:

    然后设置URL为:

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

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

    (with the leading '/' as well).

    (也有“/”这个开头)。

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

    在底层分配新的目标URL: $window.location。href = destinationUrl;

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

    强制重载,仍然处于底层:$window.location.reload();

#3


11  

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

在搜索并给出hit和try会话之后,我可以通过首先指定url来解决它

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

then reload

然后重新加载

$window.location.reload();

#4


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:

我也有同样的问题。当我使用窗口。位置,美元的窗口。位置,甚至路径不刷新页面。因此,在我的应用程序中,缓存的服务并不是我想要的,我通过添加window.location.reload()来解决它。在路由后强制页面重新加载的位置。这个方法似乎加载了页面两次。这可能是个卑鄙的伎俩,但它确实管用。这就是我现在的样子:

  $scope.openPage = function (pageName) {
      window.location = '#/html/pages/' + pageName;
      window.location.reload();
  };

#5


0  

Try this

试试这个

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

#1


179  

For <a> tags:

<一> 标记:

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

您需要在标记上粘贴target="_self"

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

有三种情况下,AngularJS将执行全页重载:

  • 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>
  • 以'/'开头的链接,当base被定义时,会导致不同的基路径。例如:link

Using javascript:

使用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.

$location服务允许您只更改URL;它不允许重载页面。当您需要更改URL并重新加载页面或导航到另一个页面时,请使用较低级别的API: $window.location.href。

See:

看到的:

#2


32  

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

我们也遇到了同样的问题,使用JS代码(即不使用HTML锚)。我们就是这样解决的:

  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.

    如果需要,可以通过$location服务修改当前URL。如果您的目的地只是当前URL的一个变体,那么这可能很有用,以便您可以利用$location helper方法。例如我们跑location.search美元(…只是更改querystring参数的值。

  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:

    如果需要,使用当前的$location.url()构建新的目标URL。为了工作,这个新的必须包括所有的东西,包括模式,域和端口。例如,如果你想:

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

    http://yourdomain.com/YourAppFolder/YourAngularApp/ / YourArea / YourAction ?文化= en

    then you should set URL as in:

    然后设置URL为:

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

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

    (with the leading '/' as well).

    (也有“/”这个开头)。

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

    在底层分配新的目标URL: $window.location。href = destinationUrl;

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

    强制重载,仍然处于底层:$window.location.reload();

#3


11  

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

在搜索并给出hit和try会话之后,我可以通过首先指定url来解决它

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

then reload

然后重新加载

$window.location.reload();

#4


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:

我也有同样的问题。当我使用窗口。位置,美元的窗口。位置,甚至路径不刷新页面。因此,在我的应用程序中,缓存的服务并不是我想要的,我通过添加window.location.reload()来解决它。在路由后强制页面重新加载的位置。这个方法似乎加载了页面两次。这可能是个卑鄙的伎俩,但它确实管用。这就是我现在的样子:

  $scope.openPage = function (pageName) {
      window.location = '#/html/pages/' + pageName;
      window.location.reload();
  };

#5


0  

Try this

试试这个

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