angular ajax loading

前端之家收集整理的这篇文章主要介绍了angular ajax loading前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

具体实现:
/**

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

})

原文链接:https://www.f2er.com/angularjs/147378.html

猜你在找的Angularjs相关文章