简体   繁体   中英

How to know that angular completed all processing and rendering

I need to stop progress bar. If I have several http request, how can i know when all threads completed, and all responses are rendered. I tried to use $httpProvider.interceptors on response, but i can not know whether this is the last http response or not. And also you can not know when this last response will be rendered.

You can track the pending request by http.pendingRequests in your interceptor like

angular.module('myApp').factory('myHttpResponseInterceptor', function($q, $location, $injector, $rootScope){
    var _http = null;

    return {
        request: function(config){
            $rootScope.$broadcast("loader_show");
            return config;
        },
        response: function(response){
            _http = _http || $injector.get('$http');
            if(_http.pendingRequests.length < 1){
                $rootScope.$broadcast("loader_hide");
            }
            return response;
        },
        responseError: function (response) {
            _http = _http || $injector.get('$http');
            if(_http.pendingRequests.length < 1){
                $rootScope.$broadcast("loader_hide");
            }
            console.log("response rejected in interceptor");
            return $q.reject(response);
        }
    };
});

I have used this in my project and it works just fine.

Hope it helps you too.

你可以使用angular-busyngProgress 之类的模块来做同样的事情。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM