I have a case where I need to repeat over results from various ajax sources. I can't wait for all sources to load before rendering, I must render the results as they come in. But I want the results to be in a particular order (alphabetical as it so happens). ngRepeat sorts them accordingly if all results are present at the same time, but not if they are loaded asynchronously.
How can I re-sort the repeat after all the results are loaded?
I've created a plunkr here to simulate the async ajax and the out of order results I get.
Here is an overly simplified, somewhat contrived sample of what I'm doing (for illustration purposes only, because SO requires code).
// ngRepeat over $scope.prices in correct order
// all ajax loaded then $scope.prices assigned
var priority = ['1 Day', '3 Day', 'Ground'];
var prices = {};
for(var i = 0; i < priority.length; ++i) {
$http.get('some/endpoint/' + priority[i])
.then(function(response) {
prices[priority[i]] = response.data;
});
}
$scope.prices = {
'1 Day': prices['1 Day'],
'3 Day': prices['3 Day'],
'Ground': prices['Ground']
};
// ngRepeat over $scope.prices in order of successful load
// $scope.prices keys assigned as results are available
var priority = ['1 Day', '3 Day', 'Ground'];
for(var i = 0; i < priority.length; ++i) {
$http.get('some/endpoint/' + priority[i])
.then(function(response) {
$scope.prices[priority[i]] = response.data;
});
}