【发布时间】: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