angular ajax loading

知識點:
AngularJS 中利用 Interceptors 來統一處理 HTTP 的錯誤css

具體實現:
/**jquery

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

define(['jquery','angular'], function($,angular){app

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);
    }
  };
});

})ide

相關文章
相關標籤/搜索