AngularJS:注入http拦截器的错误

时间:2016-05-11 03:17:01

标签: angularjs authentication http-headers interceptor

我正在使用AngularJS实现基于令牌的身份验证。令牌在服务器上创建并返回给客户端。验证后,令牌将添加到每个休息呼叫的标头中。我创建了一个authInterceptor:

ristoreApp.factory('authInterceptor', function ($rootScope, $q, $window) {
    return {
        request: function (config) {
            config.headers = config.headers || {};
            if ($window.localStorage.getItem("access_token")) {
                config.headers.Authorization = 'Bearer ' + $window.localStorage.getItem("access_token");
            }
            return config;
        },
        response: function (response) {
            if (response.status === 401) {
                // handle the case where the user is not authenticated
            }
            return response || $q.when(response);
        }
    };
}); 

然后将其注入我的config.js中,如下所示:

ristoreApp
.config(function ($httpProvider, authInterceptor, $routeProvider) {

    $httpProvider.interceptors.push('authInterceptor');

    $routeProvider
.......
})

但是我收到以下错误:

Failed to instantiate module ristoreApp due to: Unknown provider: authInterceptor

我注射拦截器的方法有什么问题?

1 个答案:

答案 0 :(得分:2)

如果未定义

,则会发生此实例化模块
  

ristoreApp

路由文件中的