角资源调用和$ Q [英] Angular Resource calls and $q
问题描述
伙计们,
我有我的code设置如下有点:
I have my code setup somewhat as below:
$scope.init = function(){
return $q.all([resource1.query(),resource2.query(),resource3.query()])
.then(result){
$scope.data1 = result[1];
$scope.data2 = result1[2];
$scope.data3 = result[3];
console.log(data1); //prints as [$resolved: false, $then: function]
doSomething($scope.data1,$scope.data2);
}
}
我是IM pression下,当所有的资源得到解决,那么功能将只调用。然而,这是不是我在code是送走。如果我打印数据1,我得到unresolveed。
I was under the impression that the "then" function will be called only when all the resources get resolved. However this is not what I am seeing in my code. If I print data1, I get unresolveed.
任何线索,以什么我是缺少在这里?
Any clue as to what I am missing here ??
推荐答案
我就遇到了这个问题,这是相当混乱。这个问题似乎是调用资源操作实际上并没有返回HTTP承诺,但一个空引用(即填充时从服务器 - 见的the $资源文档)。
I ran into this problem, and it was quite confusing. The problem appears to be that calling a resource action doesn't actually return an http promise, but an empty reference (that is populated when the data returns from the server -see the return value section of the $resource docs).
我不知道为什么,这将导致。那么(结果)返回未解决的承诺的数组,但要获得每个资源的承诺,你需要使用 resource1.query()。$承诺
。重新写你的例子:
I'm not sure why this results in .then(result) returning an array of unresolved promises, but to get each resource's promise, you need to use resource1.query().$promise
. To re-write your example:
$scope.init = function() {
return $q.all([resource1.query().$promise, resource2.query().$promise, resource3.query().$promise])
.then( function(result) {
$scope.data1 = result[0];
$scope.data2 = result[1];
$scope.data3 = result[2];
console.log($scope.data1);
doSomething($scope.data1,$scope.data2);
})
}
我希望节省了别人一些时间。
I hope that saves someone else some time.
这篇关于角资源调用和$ Q的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!