Angular 未定义的拦截器
Angular interceptors of undefined
我的 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);
}
};
})
我的 angular 版本是 1.4.4,我不知道是什么原因导致这些错误。
;
你犯了一个简单的错误:)
您错过了将 ngNotify
作为注释
在 ...,'JS_REQUIRES',
之后的注释中添加 "ngNotify"
就可以了。
我的 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);
}
};
})
我的 angular 版本是 1.4.4,我不知道是什么原因导致这些错误。
;
你犯了一个简单的错误:)
您错过了将 ngNotify
作为注释
在 ...,'JS_REQUIRES',
之后的注释中添加 "ngNotify"
就可以了。