未定义的角度拦截器

时间:2015-10-01 05:09:32

标签: javascript angularjs interceptor

我的angularjs应用程序导致错误:

TypeError: Cannot read property 'interceptors' of undefined

以下是我的设置

    var myApp = angular.module("MyApp",['ngResource','ngRoute','ui.router','ngMessages','ngResource','cfp.loadingBar','duScroll','ngAnimate','satellizer','pascalprecht.translate','ngCookies','oc.lazyLoad','ui.bootstrap','toastr','ngNotify','door3.css','ngPassword'])
        .config(['$stateProvider', '$urlRouterProvider','$controllerProvider', '$compileProvider', '$filterProvider', '$provide','$authProvider','$ocLazyLoadProvider','JS_REQUIRES','$httpProvider',function($stateProvider, $urlRouterProvider,$controllerProvider, $compileProvider, $filterProvider, $provide,$authProvider,$ocLazyLoadProvider,jsRequires,ngNotify,$httpProvider){

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

}]);

我的拦截器:

angular.module('MyApp')
  .factory('authInterceptor', function($rootScope,$q,$window) {
    return {
     request: function(config){
      config.headers = config.headers || {};
      if($window.localStorage.getItem('token')){
        config.headers.Authorization = 'Bearer ' + $window.localStorage.getItem('token');
      }
      return config;

     },

     response: function(response){
      if(response.status === 401) {
        // user not auth
      }
      return response || $q.when(response);
     }
    };
  })

我的角度版本是1.4.4,我不知道导致这些错误的原因。

1 个答案:

答案 0 :(得分:1)

你犯了一个简单的错误:)

您错过了将ngNotify作为注释

"ngNotify"之后将...,'JS_REQUIRES',添加到注释中,您将很高兴。