我正在尝试使用ui-router
$stateProvider
.state('abc', {
url: 'xyz',
templateUrl: 'templateURL',
controller: ctrl,
resolve:{
data: function(someService){
data = someService.init();
return data;
}
}
})
我的服务代码如下所示
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);
}
}
当我调试代码时,它会进入return someObject
行但是它没有解析。
我做错了什么?
答案 0 :(得分:2)
如果您希望promise3
仅在promise1
和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
});
答案 1 :(得分:0)
您必须将回调功能传递给.then(…)
。您的promise3
没有。
我怀疑你想做
return $q.all([promise1, promise2, promise3])