本文介绍了决心和q.All()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图解决类似下面使用的方法 UI路由器
I am trying to resolve a method like below using ui-router
$stateProvider
.state('abc', {
url: 'xyz',
templateUrl: 'templateURL',
controller: ctrl,
resolve:{
data: function(someService){
data = someService.init();
return data;
}
}
})
和我的服务code看起来像这样
And my service code looks like this
var someObject = {
data1: ...,
data2: ....,
...
}
return{
init: function(){
promise1 = ...
promise2 = ...
promise3 = $http.get('someurl').then(function(){
...
...
//do some manipulation with someObj
return someObject;
});
$q.all(promise1 , promise2 ).then(promise3);
}
}
当我调试code,它是来行返回someObject
但它没有解决。
When I debug the code, it is coming to line return someObject
but then it is not resolving.
我在做什么错了?
推荐答案
如果你想 promise3
后 promise1 $ C $只执行C>和
promise2
然后尝试
return $q.all([promise1, promise2])
.then(function(arrayOfData) {
return promise3;
});
下面是说明差的示例:
var promise1 = $timeout(function() {
return 'promise1Data';
}, 1000);
var promise2 = $timeout(function() {
return 'promise2Data';
}, 2000);
var promise3 = $timeout(function() {
return 'promise3Data';
}, 5000);
// This is what you're essentially doing when your route resolves
$q.all([promise1, promise2])
.then(promise3)
.then(function(data) {
console.log(data); // You get ["promise1Data", "promise2Data"]
});
// This is what I think you want
$q.all([promise1, promise2])
.then(function(arrayOfResolvedData) {
return promise3;
})
.then(function(data) {
console.log(data); // You get promise3Data
});
这篇关于决心和q.All()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!