无法读取未定义的属性“拦截器”

时间:2016-03-18 23:12:35

标签: node.js ionic-framework angular-http-interceptors angularjs-injector

我的应用程序正在连接到nodejs后端进行身份验证,工厂和配置如下

angular.module('app.services', [])
.factory('AuthInterceptor', [function($rootScope, $q, AUTH_EVENTS) {
  return {
    responseError: function(response) {
      $rootScope.$broadcast({
        401: AUTH_EVENTS.notAuthenticated,
      }[response.status], response);
      return $q.reject(response);
    }
  };
}])
.service('AuthService', [function($q, $http, API_ENDPOINT) { 
// some methods
])}
.config([function($httpProvider) {
  $httpProvider.interceptors.push('AuthInterceptor');
}]);

起始页面是一个登录页面,但我面对的是一个空白页面,其中包含控制台中的错误

 ionic.bundle.js:8900 Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module app.services due to:
TypeError: Cannot read property 'interceptors' of undefined
    at http://localhost:8100/js/services.js:94:16

服务中的第94:16行,js是:$httpProvider.interceptors.push('AuthInterceptor');

我该如何解决呢?

1 个答案:

答案 0 :(得分:0)

您忘记在调用config

时提供依赖关系字符串
.config(['$httpProvider', function($httpProvider) {
  $httpProvider.interceptors.push('AuthInterceptor');
}]);