$ location.path不与AngularJS工厂改变 [英] $location.path doesn't change in a factory with AngularJS

查看:179
本文介绍了$ location.path不与AngularJS工厂改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我厂是这样的:

'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);
      }
    }
}]);

它吐出来的日志,但不改变路径。我能做些什么来做到这一点?

It spits out the log, but doesn't change the path. What can I do to make this happen?

推荐答案

为$位置文档>说:

The documentation for $location says:

注意制定者不会立即更新window.location的。而是,$位置服务知晓的范围生命周期的和的范围中组合多个$位置突变成一个提交对window.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.

因此​​,如果拒绝承诺对 $位置的效果,那么你可能看不到预期的变化。

Hence, if the rejection of the promise has an effect on $location, then you may not see the intended change.

要强制角度生命周期之外的变化,您可以设置使用 window.location的然后强制页面的重新加载。这将,当然,停止它遵循任何code的执行和擦除会话,但可能是你想要的,如果用户没有登录。

To force a change outside of the angular life-cycle, you can set the hash using window.location and then force a reload of the page. This will, of course, stop execution of any code which follows and erase the session, but that might be what you want if the user is not logged in.

这篇关于$ location.path不与AngularJS工厂改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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