获取请求源于拦截器的范围

时间:2015-02-06 03:14:58

标签: ajax angularjs interceptor angular-http-interceptors

在AngularJS响应错误拦截器中,有没有办法检索当前请求的起源范围?

module.controller('myController', function($http, $scope) {
    $scope.getData = function() {
        // This scope is which the request is originated
        $http.get('http://www.example.com/get', {
            params: { 'id': '1234' }
        }).success(function(data, status, headers, config) {
            // ...
        }).error(function(data, status, headers, config) {
            // ...
        });
    }
});

module.factory('myInterceptor', function($q) {
    return {
        'responseError': function(response) {
            // How can I get the scope of 'myController' here?
            return $q.reject(response);
        }
    };
});

1 个答案:

答案 0 :(得分:1)

我认为您不能从开箱即用的工厂访问原始范围。 工厂是单身人士,不建议在其中使用范围。 不过,我相信你可以从控制器传递原始范围作为param对象的一部分。

 params: { 'id': '1234', 'origScope': $scope }

<强>更新

经过以下讨论...... 我认为不是在这里访问范围。 您从拦截器发布事件并让范围或关联的控制器听取它。

请参阅此链接以获取更多信息:http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/

相关问题