angular ajax loading

知识点:
AngularJS 中利用 Interceptors 来统一处理 HTTP 的错误

具体实现:
/**

  • 拦截器 全局$http注入loading效果
    */

define([‘jquery’,’angular’], function($,angular){

  
angular.module('ajaxLoading', [])

.config(function($httpProvider) {
  $httpProvider.interceptors.push('loadingInterceptor');
})
 
.directive('loading', function() {
  return {
    replace: true,
    restrict: 'AE',
    template:'<div class="back-layer"><div class="loading">'
            +'<img src="images/729.GIF">'
            +'</div></div>',
    link: function($scope, $element, attrs) {
        var top = $(window).height()/2 - 25;
        var left = $(window).width()/2 - 25;
        $('.loading').css({
          top: top,
          left: left
        });
        //$(tpl).appendTo('body');
    }
  };
})
 
.factory('loadingInterceptor', function($q, $rootScope) {

  return {
    request: function(config) {
      $(".back-layer").show();
      return config || $q.when(config);
    },
    response: function(response) {
      $(".back-layer").hide();
      return response || $q.when(response);
    },
    responseError: function(rejection) {
      $(".back-layer").hide();
      return $q.reject(rejection);
    }
  };
});

})

    原文作者:cfang
    原文地址: https://segmentfault.com/a/1190000009261529
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞