Angularjs 当数据与多个http调用异步时,如何对列表进行排序
我需要循环URL列表,并在一个表中显示结果。 看起来,Angularjs 当数据与多个http调用异步时,如何对列表进行排序,angularjs,Angularjs,我需要循环URL列表,并在一个表中显示结果。 看起来,orderBy不起作用,因为list($scope.versions)来自多个HTTP调用,并且HTTP是异步的 控制器: var callApi = function (app, env, index, url) { $http.get(url). success(function(data) { data.pos=index; $scope.versions[app].push([data]);
orderBy
不起作用,因为list($scope.versions)
来自多个HTTP调用,并且HTTP是异步的
控制器:
var callApi = function (app, env, index, url) {
$http.get(url).
success(function(data) {
data.pos=index;
$scope.versions[app].push([data]);
}).
error(function(data, status) {
var errorData = {};
errorData.pos=index;
$scope.versions[app].push([errorData]);
});
console.log($scope.versions);
};
HTML:
{{k}
[{{item[0].pos}]
有点不清楚您到底想做什么,也不清楚您的数据是什么样子的,但从一般意义上讲,您总是可以在收到数据时对其进行排序
例如,可以使用splice()
将数据插入阵列中需要的位置:
$scope.items = [];
var urls = [ <some urls> ];
urls.forEach( function (url) {
$http.get(url)
.success( function (data) {
var position = getPosition(data);
$scope.items.splice(position, 0, data);
})
.error( function (data, status) {
// Handle the error case
});
});
function getPosition (data) {
// Some code to find where the element
// should be inserted into the array
return position;
}
$scope.items=[];
var url=[];
forEach(函数(url){
$http.get(url)
.成功(功能(数据){
var位置=getPosition(数据);
$scope.items.splice(位置,0,数据);
})
.错误(功能(数据、状态){
//处理错误案例
});
});
函数getPosition(数据){
//查找元素所在位置的一些代码
//应该插入到数组中
返回位置;
}
或者,您可以在每次添加新数据后对数据进行排序:
$scope.items = [];
var urls = [ <some urls> ];
urls.forEach( function (url) {
$http.get(url)
.success( function (data) {
$scope.items.push(data);
$scope.items.sort(itemsCompareFunc)
})
.error( function (data, status) {
// Handle the error case
});
});
function itemsCompareFunc (a, b) {
// Some logic to compare both items
}
$scope.items=[];
var url=[];
forEach(函数(url){
$http.get(url)
.成功(功能(数据){
$scope.items.push(数据);
$scope.items.sort(itemsCompareFunc)
})
.错误(功能(数据、状态){
//处理错误案例
});
});
功能项比较(a、b){
//比较这两项的一些逻辑
}
有点不清楚您到底想做什么,也不清楚您的数据是什么样子的,但从一般意义上讲,您总是可以在收到数据时对其进行排序
例如,可以使用splice()
将数据插入阵列中需要的位置:
$scope.items = [];
var urls = [ <some urls> ];
urls.forEach( function (url) {
$http.get(url)
.success( function (data) {
var position = getPosition(data);
$scope.items.splice(position, 0, data);
})
.error( function (data, status) {
// Handle the error case
});
});
function getPosition (data) {
// Some code to find where the element
// should be inserted into the array
return position;
}
$scope.items=[];
var url=[];
forEach(函数(url){
$http.get(url)
.成功(功能(数据){
var位置=getPosition(数据);
$scope.items.splice(位置,0,数据);
})
.错误(功能(数据、状态){
//处理错误案例
});
});
函数getPosition(数据){
//查找元素所在位置的一些代码
//应该插入到数组中
返回位置;
}
或者,您可以在每次添加新数据后对数据进行排序:
$scope.items = [];
var urls = [ <some urls> ];
urls.forEach( function (url) {
$http.get(url)
.success( function (data) {
$scope.items.push(data);
$scope.items.sort(itemsCompareFunc)
})
.error( function (data, status) {
// Handle the error case
});
});
function itemsCompareFunc (a, b) {
// Some logic to compare both items
}
$scope.items=[];
var url=[];
forEach(函数(url){
$http.get(url)
.成功(功能(数据){
$scope.items.push(数据);
$scope.items.sort(itemsCompareFunc)
})
.错误(功能(数据、状态){
//处理错误案例
});
});
功能项比较(a、b){
//比较这两项的一些逻辑
}
您能否给出一个问题陈述,询问您需要在问题正文中解决的具体问题?谢谢。请给出一个问题陈述,询问您需要在问题主体中解决的具体问题?谢谢