问题
I'm trying to resolve a promise array to things
before hitting my controller:
resolve:{
things: function($q){
var promises = [];
var titles = [];
var thingRef = ['a1', 'a2', 'a3'];
angular.forEach(thingRefs, function(thingRef){
promises.push($firebase(ref.child('things').child(thingRef).child('title')).then(function(title){
titles.push(title);
}));
});
$q.all(promises).then(function(){
return titles;
});
}
},
What am I doing wrong here?
回答1:
I think you need:
return $q.all(promises).then(function(){
return titles;
});
Because without that outer return the inner return doesn't go anywhere.
Now resolve.things
returns a promise, that when resolved, will contain an Array of titles.
With some other adjustments:
resolve:{
things: function($q){
var promises = [];
var thingRef = ['a1', 'a2', 'a3'];
angular.forEach(thingRefs, function(thingRef){
promises.push($firebase(ref.child('things').child(thingRef).child('title')));
});
return $q.all(promises);
}
}
来源:https://stackoverflow.com/questions/23572801/resolve-a-promise-array-with-angular-ui-router