为什么在promise中设置$ window.location.href时不起作用? [英] Why setting $window.location.href does not work when set inside a promise?

查看:282
本文介绍了为什么在promise中设置$ window.location.href时不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将请求发送到服务器,并希望在收到响应后有条件地重定向到另一个页面(非角度).因此导航发生在Promise的then部分内.

I send request to the server and want conditionally redirect to another page (not angular) after response is received. Thus navigation happens inside then part of a promise.

我尝试过:

I tried:

$location.path(url)

$window.location.href = url;
$windo.location.reload();

什么都行不通.

但是如果我用setTimeout(navigate,0)包裹这两个调用中的任何一个,则会发生重定向.
似乎在摘要周期中,Angular Guard会出现网址.

But if I wrap either of these two calls with setTimeout(navigate,0) redirection occurs.
Seems like Angular guards url during digest cycle.

任何人都可以澄清或共享解释实际情况的链接.

Can anyone clarify or share the links explaining what really happens.

推荐答案

进行更改之后,并在结束Promise处理程序之前,尝试执行以下操作:

After doing the change, and before ending the promise handler, try doing:

$scope.$$phase || $scope.$apply();

这应该填充更改.

这篇关于为什么在promise中设置$ window.location.href时不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆