角JS - 检测当所有的$ HTTP()已经完成 [英] Angular js - detect when all $http() have finished
本文介绍了角JS - 检测当所有的$ HTTP()已经完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我有吨 $ HTTP的()
要求周围所有的应用程序code,
Ok i have tons of $http()
calls all around the app code,
我想知道是否有检测当所有 $ HTTP()
围绕应用程序已经完成(成功/错误donesn't什么办法/最佳实践无论他们如何返回时,只需要知道,如果成品)?
i'm wondering is there any way / best practice to detect when all $http()
around the app have finished ( success/error donesn't matter what they return, just need to know if finished )?
这样我就可以显示加载GIF,直到它们被装?
So that i can show a loading gif until they are loading ?
感谢
推荐答案
做这样的:
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
事件的任何地方(preferable指令中使用):
Then use event bound to $rootScope
anywhere (preferable to use in directive):
$rootScope.$on('loading:progress', function (){
// show loading gif
});
$rootScope.$on('loading:finish', function (){
// hide loading gif
});
这篇关于角JS - 检测当所有的$ HTTP()已经完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文