Angular白名单&&Angular拦截器 全局通用

时间:2022-07-21 06:50:55

//angular 白名单全局通用
app.config([
  '$compileProvider',
  function ($compileProvider) {
  $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|itms-services|ftp|javascript|mailto|tel|file|sms):/);
  // Angular v1.2 之前使用 $compileProvider.urlSanitizationWhitelist(...)
  }
]);

//angular拦截器 声明
app.config([ '$httpProvider', function($httpProvider) {
  $httpProvider.interceptors.push('httpInterceptor');
}]);

//使用拦截器
app.factory('httpInterceptor', [ '$q', '$injector',function($q, $injector) {
  var httpInterceptor = {
  'responseError' : function(response) {
//401情况下
  if (response.status == 401){
  var rootScope = $injector.get('$rootScope');
  var state = $injector.get('$rootScope').$state.current.name;
  rootScope.stateBeforLogin = state;
  rootScope.$state.go("login");
  return $q.reject(response);
  //不等于200情况下
} else if (response.status != 200){
  showMsg(message,'center');
  removeLoading("test");
  return $q.reject(response);
}
},
  'response' : function(response) {
  return response;
}
}
  return httpInterceptor;
}
]);