加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在使用AngularJS的工厂中,$location.path不会更改

发布时间:2020-12-17 07:56:25 所属栏目:安全 来源:网络整理
导读:我的工厂看起来像: 'use strict';angular.module('myApp') .factory('httpInterceptor',['$q','$location','$rootScope',function($q,$location,$rootScope){ return { response: function(response) { if(response.success === false) {console.log("Redir
我的工厂看起来像:
'use strict';

angular.module('myApp')
  .factory('httpInterceptor',['$q','$location','$rootScope',function($q,$location,$rootScope){
    return {
      response: function(response) {

        if(response.success === false) {
console.log("Redirecting");
            $location.path('/login');
            return $q.reject(response);
        }

        return response || $q.when(response);
      }
    }
}]);

它会吐出日志,但不会改变路径.我该怎么做才能实现这一目标?

documentation for $location说:

Note that the setters don’t update window.location immediately. Instead,the $location service is aware of the scope life-cycle and coalesces multiple $location mutations into one “commit” to the window.location object during the scope $digest phase.

因此,如果拒绝承诺对$location有影响,那么您可能看不到预期的变化.

要强制在角度生命周期之外进行更改,可以使用window.location设置哈希,然后强制重新加载页面.当然,这将停止执行后面的任何代码并擦除会话,但如果用户未登录,则可能是您想要的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读