我试图迭代我在angular.forEach()内的多个http调用构造的数组
$scope.ticket_stats = function(){ //cleaning variables $scope.data_set = []; $scope.closed_tickets = []; //fetching time stamps (epoch) $scope.time_frame = time_period.days(7); //calling data using time stamps angular.forEach($scope.time_frame,function(item) { //debug console.log(item); var promise = tickets.status("closed",item); promise.success(function(data){ console.log(data); $scope.closed_tickets.push(data[0].datapoints[0][0]); // returns a numerical value }); }); //SEE MESSAGE BELOW $scope.data_set.push($scope.closed_tickets); }
$scope.data_set.push()的最后一行正在工作,但一旦调用返回数据就会随着时间的推移自行递增.我想在for Each循环中的所有内容都完成之后执行此行.我需要迭代$scope.closed_tickets数组,然后在其中播放(添加)数据并构建第二个数组.
以下是此功能中使用的服务:
// CALL TICKETS STATS app.service('tickets',function($http){ this.status = function(status,date){ var one_snap = date - 100; var url = "/url/render?format=json&target=sum(stats.tickets."+status+")&from="+one_snap+"&until="+date+""; return $http.get(url); }; }); // TIME STAMPS MATHS app.service('time_period',function(){ var currentDate = parseInt((new Date).getTime()/1000); this.days = function(number){ var pending = []; for (var i = number; i > 0; i--) { pending.push(currentDate - (87677*i)); } return pending; }; });
我搜索信息并发现有关$q.all()服务的信息,但没有设法以我想要的方式完成这项工作.
任何建议都会受到欢迎!
谢谢!
您可以使用$q.all等待多个异步事件(promises)完成.
$scope.ticket_stats = function() { // list of all promises var promises = []; //cleaning variables $scope.data_set = []; $scope.closed_tickets = []; //fetching time stamps (epoch) $scope.time_frame = time_period.days(7); //calling data using time stamps angular.forEach($scope.time_frame,function(item) { // create a $q deferred promise var deferred = $q.defer(); //debug console.log(item); tickets.status("closed",item).success(function(data) { console.log(data); $scope.closed_tickets.push(data[0].datapoints[0][0]); // promise successfully resolved deferred.resolve(data); }); // add to the list of promises promises.push(deferred.promise); }); // execute all the promises and do something with the results $q.all(promises).then( // success // results: an array of data objects from each deferred.resolve(data) call function(results) { $scope.data_set.push($scope.closed_tickets); },// error function(response) { } ); }
首先,deferred代表一段代码,它将花费不明的时间执行(异步). deferred.resolve(data)简单地声明代码已完成.数据可以是任何东西,对象,字符串等等,但它通常是异步代码的结果.同样,您可以使用deferred.reject(数据)拒绝承诺(可能是服务器引发了错误).同样,数据可以是任何东西,但在这里它可能应该是错误响应.
deferred.promise只返回一个promise对象. promise对象允许您设置回调,如.then(successFunction,errorFunction),因此您知道一段代码已经完成执行,然后转到successFunction(或者在失败的情况下为errorFunction).在我们的例子中,$q有.all方法,它等待一个promises数组完成然后给你作为数组的所有promises的结果.
不要忘记注入$q服务.