AngularJS - 在配置中调用服务

时间:2013-11-20 18:36:16

标签: angularjs

我想在AuthenticationService.logout()上致电401 http error。但是,我无法使其发挥作用。我想我不能为配置注入服务,但我怎么能实现这个呢?

myApp.config(['$httpProvider', 'AuthenticationService', function ($httpProvider, AuthenticationService) {
    var interceptor = ['$rootScope','$q', function(scope, $q) {

        function success(response) {
            return response;
        }

        function error(response) {
            var status = response.status;

            if (status == 401) {
                var deferred = $q.defer();
                var req = {
                    config: response.config,
                    deferred: deferred
                }

                /* LOGOUT HERE */
                AuthenticationService.logout();

                return deferred.promise;
            }

            return $q.reject(response);

        }

        return function(promise) {
            return promise.then(success, error);
        }

    }];
    $httpProvider.responseInterceptors.push(interceptor);

}]);

我做错了什么,我该如何解决?

0 个答案:

没有答案