问题
My $q.all() is working fine. The below code is in my primary service and when done everything is returned to the controller correctly. However I am trying to pre-sort the final array returned to the controller its not working. The sort function itself works - its the identical function in my controller that I use to manually sort the results on my template page. Its just the initial results don't come back sorted.
I know its an async issue, but can't figure out where the sort needs to go before the results are returned.
// SERVICE
var groups = [] ;
$q.all([promise1(),promise2(),promise3(),promise4(),promise5(),promise6()]).then(function(response){
var z=0 ;
for (var y=0;y<response.length;y++) {
// process each promise as a complete
// value: [0][{a:1,b:2,c:3},{a:4,b:5,c:6}],
// [1]:[{a:7,b:8,c:9},{a:10,b:11,c:12}]
for (var x=0;x<response[y].length;x++) {
// process the components of each promise
// value [{a:1,b:2,c:3},{a:4,b:5,c:6}]
response[y][x].ID = z++ ; // reassign primary 'list' ID
groups.push(response[y][x]) ; // push preformatted component
// to 'groups' to be passed
// back to controller
}
}
var sortType = getDB("sortOption") ;
sortGroups(sortType) ; // global 'groups' is already
// in 'sortGroups()'
});
return {
all: function() {
return [groups,sortType] ;
}
// CONTROLLER
$scope.groups = ServiceGroups.all() ;
In the controller, 'groups' is always returned, just not sorted.
来源:https://stackoverflow.com/questions/46494215/sorting-q-all-responses-not-working