像这样做:
angular.module('app').factory('httpInterceptor', ['$q', '$rootScope', function ($q, $rootScope) { var loadingCount = 0; return { request: function (config) { if(++loadingCount === 1) $rootScope.$broadcast('loading:progress'); return config || $q.when(config); }, response: function (response) { if(--loadingCount === 0) $rootScope.$broadcast('loading:finish'); return response || $q.when(response); }, responseError: function (response) { if(--loadingCount === 0) $rootScope.$broadcast('loading:finish'); return $q.reject(response); } }; }]).config(['$httpProvider', function ($httpProvider) { $httpProvider.interceptors.push('httpInterceptor');}]);
然后使用绑定到
$rootScope任何地方的事件(最好在指令中使用):
$rootScope.$on('loading:progress', function (){ // show loading gif});$rootScope.$on('loading:finish', function (){ // hide loading gif});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)