完成多个http时的角度回调

时间:2015-08-20 14:26:46

标签: angularjs http callback

如何为angularjs中的多个http创建1个回调函数。我的代码:

for (var i = 0; i < docs.length; i++) {
    this.base64(docs[i], function(base64Img){
        $http.post(urls.BASE + '/store',{doc:base64Img}).then(
            function(result) {
                console.log(result);
            }
         );
     });
 }

 mycallback.call(); <-- this should be done when all my http.post above are done.

2 个答案:

答案 0 :(得分:0)

好的,所以我为此创建了一个HttpInterceptor服务,所以每次请求开始时它会检查在某个时间限制内是否有更多请求,然后在所有这些请求响应之后它广播&#34;所有请求都已完成&#34;。

为了实现这一点,我在我的App.js

中嵌入了我的Intercepter
.config(function ($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptor');
})

我的服务看起来像这样基本上有一个变量numloadings来计算请求,当有响应时它会在它达到0时将它们计算下来所有请求都通过

.factory('httpInterceptor', ['$q', '$rootScope', '$filter', function ($q, $rootScope, $filter) {
var canceller = $q.defer();
var numLoadings = 0;
var serialRequests = false;
var timeO;
var time1;
var Cntr1 = 0;
var Cntr2 = 0;
var currentReqUrl;
$rootScope.broadcast = true;
var loadingbar = { loading: "<progress value='?' max='10'></progress>" };
var loadingspinner = { loading: '<ion-spinner icon="crescent"></ion-spinner>' };

return {

    request: function (config) {

            config.timeout = 200000;



            numLoadings++;
            if (serialRequests == false) {

                $rootScope.$broadcast("open_requests", loadingspinner);
            } else {
                clearTimeout(timeO);
            }
        }

        return config || $q.when(config)
    },

    response: function (response) {

            serialRequests = true;
            numLoadings--;

            timeO = setTimeout(function () {
                serialRequests = false
                if ((numLoadings) === 0) {
                    $rootScope.$broadcast("all_requests_done");
                }
            });
        }
        return response || $q.when(response);
    },

    responseError: function (response) {

            serialRequests = true;
            numLoadings--;

            timeO = setTimeout(function () {
                serialRequests = false
                if ((numLoadings) === 0) {
                    $rootScope.$broadcast("all_requests_done");
                }
            });
        }
        return $q.reject(response);
    }


 };

答案 1 :(得分:0)

使用$q.all()

var deferred = $q.defer();
var httpPromises = [];
for (var i = 0; i < docs.length; i++) {
    this.base64(docs[i], function(base64Img) {
        httpPromises.push($http.post(urls.BASE + '/store',{doc:base64Img});
        if (httpPromises.length === docs.length) {
            deferred.resolve();
        }
    }));
}

return deferred.promise.then(function() {
    return $q.all(httpPromises);
});

请注意,如果this.base64()返回了一个promise而不是在参数中进行回调,那会更简单:

var promises = [];
for (var i = 0; i < docs.length; i++) {
    promises.push(this.base64(docs[i]).then(function(base64Img) {
        return $http.post(urls.BASE + '/store',{doc:base64Img});
    }));
}
return $q.all(promises);

甚至

return $q.all(docs.map(function(doc) {
    return this.base64(doc).then(function(base64Img) {
        return $http.post(urls.BASE + '/store',{doc:base64Img});
    });
});