【问题标题】:How to inject my service into interceptor?如何将我的服务注入拦截器?
【发布时间】:2019-01-09 19:59:55
【问题描述】:

我创建了如下拦截器:

(function() {
    'use strict';
    angular.module('sbApp').factory('ErrorResponseInterceptor', ErrorResponseInterceptor).config(ErrorResponseInterceptorConfig);

    ErrorResponseInterceptorConfig.$inject = [ '$httpProvider' ];
    ErrorResponseInterceptor.$inject = [ '$rootScope', '$q', '$injector'];

    function ErrorResponseInterceptorConfig($httpProvider) {
        $httpProvider.interceptors.push('ErrorResponseInterceptor');
    }

    function ErrorResponseInterceptor($rootScope, $q, $injector) {
    // Here I handle my http responses

    }
})();

我在文件myService.js中定义了其他服务我想在上面的拦截器中使用这个服务的方法,所以我对上面的代码做了以下更改:

(function() {
    'use strict';
    angular.module('sbApp').factory('ErrorResponseInterceptor', ErrorResponseInterceptor).config(ErrorResponseInterceptorConfig);

    ErrorResponseInterceptorConfig.$inject = [ '$httpProvider' ];
    ErrorResponseInterceptor.$inject = [ '$rootScope', '$q', '$injector', 'myService'];

    function ErrorResponseInterceptorConfig($httpProvider) {
        $httpProvider.interceptors.push('ErrorResponseInterceptor');
    }

    function ErrorResponseInterceptor($rootScope, $q, $injector, myService) {
        // Here I handle my http responses

    }
})();

我有以下错误:

Uncaught Error: [$injector:unpr] Unknown provider: myServiceProvider <- myService <- ErrorResponseInterceptor <- $http <- $translateStaticFilesLoader

myService.js 代码:

(function() {
    'use strict';
    angular.module('sbApp').factory('myService', myService);

    myService.$inject = [ '$rootScope', '$http', 'restAPIService', '$log',
            '$filter', '$interval', '$location' ];

        function myService($rootScope, $http, restAPIService, $log, $filter,
                $interval, $location) {
                ......
            return {
                add : add,
            };
            .....
            function add(module, event) {
            var e = {
              ..........
            }
            .......     
                return $rootScope.myarray.push(e);
            }

        }
    })();

是否允许在拦截器中使用myService,如何将其传递给拦截器?

【问题讨论】:

  • @Downvoters 也请添加解释,以便像我这样的 Angular js 新手可以根据需要更新问题
  • 可以添加我的服务代码吗?
  • @Karim 请看看我更新的问题,

标签: javascript angularjs angular-http-interceptors


【解决方案1】:

要在拦截器中获取服务对象的实例,请使用:

 $injector.get('serviceName');

请尝试以下方法:

(function() {
    'use strict';
    angular.module('sbApp').factory('ErrorResponseInterceptor', ErrorResponseInterceptor).config(ErrorResponseInterceptorConfig);

    ErrorResponseInterceptorConfig.$inject = [ '$httpProvider' ];
    ErrorResponseInterceptor.$inject = [ '$rootScope', '$q', '$injector'];

    function ErrorResponseInterceptorConfig($httpProvider) {
        $httpProvider.interceptors.push('ErrorResponseInterceptor');
    }

    function ErrorResponseInterceptor($rootScope, $q, $injector) {
        var myService = $injector.get('myService');
        // Here I handle my http responses

    }
})();

【讨论】:

    猜你喜欢
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2020-05-08
    • 1970-01-01
    • 2012-02-09
    • 2020-06-03
    相关资源
    最近更新 更多