我的工厂看起来像:
'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设置哈希,然后强制重新加载页面.当然,这将停止执行后面的任何代码并擦除会话,但如果用户未登录,则可能是您想要的.