我正在开发一个包含多个模块的应用程序。我正在使用服务模块和根模块。我在服务模块中定义了一个工厂。我在root模块中调用$httpProvider.interceptors.push("authInterceptorService")
。这条线投掷错误。
Error: [$injector:unpr] Unknown provider: authInterceptorServiceProvider <- authInterceptorService <- $http <- $templateRequest <- $compile
http://errors.angularjs.org/1.4.1/$injector/unpr?p0=authInterceptorServiceProvider%20%3C-%20authInterceptorService%20%3C-%20%24http%20%3C-%20%24templateRequest%20%3C-%20%24compile
代码:
angular.module('app', ['ngAnimate', 'ui.router','app.data'])
.config(["$stateProvider", "$urlRouterProvider", "$httpProvider", function ($stateProvider, $urlRouterProvider, $httpProvider) {
$stateProvider.state("/", {
url: "/",
views: {
content: { templateUrl: "/Scripts/app/Views/Splash.html", controller: "SplashController" }
}
});
$urlRouterProvider.otherwise("/");
$httpProvider.interceptors.push("authInterceptorService");
}]);
--- Service module
angular.module("app.data",[])
.factory("authInterceptorService", ["$q", "$injector", "localStorageService", function ($q, $injector, localStorageService) {
var request = function (config) {
config.headers = config.headers || {};
var authData = localStorageService.get("authorizationData");
if (authData) {
config.headers.Authorization = "Bearer " + authData.token;
}
return config;
}
var responseError = function (rejection) {
if (rejection.status === 401) {
var stateService = $injector.get("$state");
stateService.go("Login");
}
return $q.reject(rejection);
}
var authInterceptorServiceFactory = {
request: request,
responseError : responseError
};
return authInterceptorServiceFactory;
}]);