Angularjs 是否执行循环而不是多个$http链接?
对不起,我已经检查了答案,但仍然不知道如何做: 我有一个数组:Angularjs 是否执行循环而不是多个$http链接?,angularjs,json,ajax,Angularjs,Json,Ajax,对不起,我已经检查了答案,但仍然不知道如何做: 我有一个数组: var surfaces = [ {min:0,max:499}, {min:500,max:999}, {min:1000,max:1499}, {min:1500,max:1999}, {min:2000,max:2399}, {min:2400,max:2999}, {min:3000,max:3999}, {min:4000,ma
var surfaces = [
{min:0,max:499},
{min:500,max:999},
{min:1000,max:1499},
{min:1500,max:1999},
{min:2000,max:2399},
{min:2400,max:2999},
{min:3000,max:3999},
{min:4000,max:5999},
{min:6000,max:100000}
]
我有一个$q的looong$http链接调用:
$q.when()
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[0]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[1]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[2]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[3]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[4]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[5]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[6]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[7]).success(function(data){
$scope.normes.push(data);
})
})
.then(function () {
$http.post('backend/backend.php?action=get_normes',surfaces[8]).success(function(data){
$scope.normes.push(data);
})
})
我宁愿做一个循环,但现在已经有了如何做这件事的想法
编辑:我有一个更大的问题:数据在$scope.normes中不有序!它总是不同的!我怎样才能一直按顺序推?数据怎么可能是有序的
我得到了类似这样的ng重复,但由于$http synchornicity,信息的顺序不是很好
<div class="row ">
<div class="col-lg-10" style="overflow: auto; max-height: 600px;">
<table class="table table-bordered table-hover " border="1" >
<thead >
<tr>
<th> Désignation</th>
<th> Total Magasins</th>
<th> Moy Habitants Zone</th>
<th> Ca Moyen</th>
<th> EO Mini</th>
<th> EO Maxi</th>
<th> Coef Moyen</th>
<th> Pourcent Côtier</th>
<th> Pourcent Urbain</th>
<th> Tx Habituels</th>
<th> Tx fréquentation</th>
<th> Tx fidélisation</th>
<th> Zone Attraction</th>
<th> Revenus Zone</th>
<th> Nb Perso Foyer</th>
<th> Age Moyen</th>
</tr>
</thead>
<tbody >
<tr ng-repeat="norme in normes" >
<td>Surface 0-499 m²</td>
<td>{{::norme.nb_magasins | number:0}}</td>
<td>{{::norme.moy_habitants_zone | number:0}}</td>
<td>{{::norme.ca_moyen | number:0}}</td>
<td>{{::norme.eomin | number:0}}</td>
<td>{{::norme.eomax | number:0}}</td>
<td>{{::norme.coef_moyen | number:2}}</td>
<td></td>
<td></td>
<td>{{::norme.tx_habituels | number:2}}</td>
<td>{{::norme.tx_frequentation | number:2}}</td>
<td>{{::norme.tx_fidelisation | number:2}}</td>
<td>{{::norme.attraction | number:0}}</td>
<td>{{::norme.revenus_zone | number:0}}</td>
<td>{{::norme.nb_pers_foyer | number:2}}</td>
<td>{{::norme.age_moyen | number:2}}</td>
<td ></td>
</tr>
</tbody>
</table>
</div>
签名
总马加辛
莫伊居住区
卡莫延
EO迷你型
EO Maxi
科夫·莫延
香喷喷
波尔森乌尔班
哈比图埃尔斯酒店
传讯
Tx fidélisation
区域吸引力
雷文努斯区
Nb个人休息室
老莫言
表面0-499 m²
{{::norme.nb_magasins |编号:0}
{{::norme.moy_居住区{124;编号:0}
{{::norme.ca|moyen |编号:0}
{{::norme.eomin |编号:0}
{{::norme.eomax |编号:0}
{{::norme.coef|moyen |编号:2}
{{::norme.tx_habituels |编号:2}
{{::norme.tx|u频繁光顾次数:2}
{{::norme.tx|fidelisation{编号:2}
{{::norme.attraction | number:0}
{{::norme.revenus_区|编号:0}
{{::norme.nb_pers_foyer |编号:2}
{{::norme.age_moyen |编号:2}
每次我重做$q,它都会带来不同的订单!我怎么办
EDIT:因此我现在从后端获取标准JSON对象,因此它更简单(编辑了本文中的html表),但是使用您温和提供的解决方案,它还没有按正确的顺序出现。$http以正确的顺序启动,但是$scope.normes似乎没有列出,因为$http已经启动了!(哦,我想我可能可以在前端使用orderby进行订购……我忘了,但我认为可以在json对象被推入数组时对其进行订购,但在视图中,它看起来与调用$http时不同)尝试以下操作:
$scope.onAllDone = function() {
var promises = [];
angular.forEach(surfaces , function(surface) {
promises.push($http.post('backend/backend.php?action=get_normes',surface)).then(function(data) {
$scope.normes.push(data);
return data;
});
});
return $q.all(promises);
}
USE::
$scope.onAllDone().then(fnSuccess, fnError);
承诺和$q.all()是你的朋友
更详细地说,您必须为每个调用做出承诺(如果调用本身没有返回一个),将它们放入数组中并调用$q.all(promises)。然后(allFinished)
函数更新(x,承诺){
var d=$q.defer();
$http.post('backend/backend.php?action=get_normes',x)。然后(函数(resp){
$scope.normes.push(相应数据);
d、 解析(相应数据);
})
承诺。推动(d.承诺);
}
...
var承诺=[];
$scope.normes=[];
对于(var i=0;i
创建一系列承诺
var promises = [];
surfaces.forEach(function(surface){
promises.push($http.post('backend/backend.php?action=get_normes',surface));
})
您可以使用$q.all()
并等待所有承诺完成,然后使用创建$scope.normes
数组
$q.all(promises).then(function (results) {
$scope.normes = [].concat.apply([],results);
});
链接承诺时,重要的是将后续承诺返回给
。然后
方法处理程序函数:
var arrayPromise = $q.when([])
.then(function (dArray) {
var httpPromise = $http.post('backend/backend.php/action=get_normes',surfaces[0])
var dPromise = httpPromise.then(function(response) {
$scope.normes.push(response.data);
dArray.push(response.data);
return dArray;
});
return dPromise;
}).then(function(dArray) {
console.log("one XHR is complete");
console.log(dArray);
});
当代码未能向处理函数返回任何内容时,$q服务将承诺解析为未定义
,并立即执行下一个函数。通过返回后续承诺,$q服务将等待承诺得到解决,然后再执行链中的下一个承诺
也就是说。如何创建链接连续承诺的循环
var arrayPromise = $q.when([]);
for (let n=0; n<surfaces.length; i++) {
arrayPromise = arrayPromise
.then(function (dArray) {
var httpPromise = $http.post(url,surfaces[n]);
var dPromise = httpPromise.then(function(response) {
$scope.normes.push(response.data);
dArray.push(response.data);
//return dArray to chain
return dArray;
});
return dPromise;
});
};
arrayPromise.then(function(dArray) {
console.log("all XHRs are complete");
console.log(dArray);
}).catch(function(errorResponse) {
console.log("one of the XHRs failed");
console.log(errorResponse.status);
throw errorResponse;
});
var arrayPromise=$q.when([]);
对于(设n=0;您可以使用angular内置函数forEach
来处理多次异步http调用。我尝试了很多次。很抱歉,我遇到了此错误,错误:promises.push(…)。then不是一个函数。但它似乎是一个很棒的代码。谢谢。是的,它工作得很好,非常感谢!不幸的是,我没有以正确的顺序得到结果,每次都是以不同的顺序。我想,因为我在数组上做ng重复,以数字作为索引…它似乎正在进入$scope.normes每次都是不同的顺序,给出不同的索引,我甚至不明白怎么做,因为有一个承诺,那应该是按正确的顺序推进的。很高兴它能起作用。:)。哦,您可以在ng RepeatNG中使用orderBy过滤器,因为您知道$http.post是异步的。所以,你永远不会知道哪一个1是第一个完成的。你可以在得到所有结果后再进行排序。好吧,我现在尝试在后端创建对象而不是数组,这毫无意义,我将创建另一个问题,因为我不知道如何在同一个表中对一个对象数组重复几次
var arrayPromise = $q.when([]);
for (let n=0; n<surfaces.length; i++) {
arrayPromise = arrayPromise
.then(function (dArray) {
var httpPromise = $http.post(url,surfaces[n]);
var dPromise = httpPromise.then(function(response) {
$scope.normes.push(response.data);
dArray.push(response.data);
//return dArray to chain
return dArray;
});
return dPromise;
});
};
arrayPromise.then(function(dArray) {
console.log("all XHRs are complete");
console.log(dArray);
}).catch(function(errorResponse) {
console.log("one of the XHRs failed");
console.log(errorResponse.status);
throw errorResponse;
});