在Angular中,我如何通过所有服务调用都返回其停止加载栏的响应
最好的解决方案之一是实现拦截器。拦截器是一家拦截所有请求&的工厂。响应是从您的角应用程序触发的。
app.factory( 'Inteceptor', function($rootScope) {
int count = 0;
return {
request : function() {count++},
requestError : function() {},
reponse : function() {
count--;
if( count == 0 ) {
$rootScope.$broadcast( 'Event' )
}
},
responseError : function() {}
}});
在此拦截器中,您可以保留发送总请求的计数。从零开始,在RequeSet上增加IT并将响应减少。要知道何时完成所有请求,您可以简单地播放一个事件,让所有人都知道所有需求都已满足。
这就是您在config方法中推动拦截器的方式。
$httpProvider.interceptors.push('CustomHttpInterceptor');