2016-05-11 155 views
1

我正在用AngularJS实现基于令牌的认证。令牌在服务器上创建并返回给客户端。认证后,令牌将被添加到每个其他呼叫的标题中。我创建了一个authInterceptor:AngularJS:错误注入http拦截器

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 

有什么不对我的路注入拦截器?

+0

你能发表你的详细代码吗?如果你添加任何小提琴/ plnkr会更好。 –

回答

2

这无法实例化模块发生在你还没有在你的路由文件中定义

ristoreApp