angularjs – 角度资源调用和$q

前端之家收集整理的这篇文章主要介绍了angularjs – 角度资源调用和$q前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
伙计们,

我的代码设置有些如下:

  1. $scope.init = function(){
  2. return $q.all([resource1.query(),resource2.query(),resource3.query()])
  3. .then(result){
  4. $scope.data1 = result[1];
  5. $scope.data2 = result1[2];
  6. $scope.data3 = result[3];
  7.  
  8.  
  9. console.log(data1); //prints as [$resolved: false,$then: function]
  10.  
  11. doSomething($scope.data1,$scope.data2);
  12. }
  13. }

我的印象是,只有当所有资源得到解决时,才会调用“then”函数。不过这不是我在代码中看到的。如果我打印数据1,我得到解析。

任何关于我在这里失踪的线索?

@H_502_12@ 我遇到这个问题,这很混乱。问题似乎是调用资源操作实际上并没有返回一个http承诺,而是一个空的引用(当数据从服务器返回时被填充) – 参见 the $resource docs的返回值部分。

我不知道为什么这会导致。(结果)返回一系列未解决的承诺,但为了获得每个资源的承诺,您需要使用resource1.query()$ promise。重写你的例子:

  1. $scope.init = function() {
  2. return $q.all([resource1.query().$promise,resource2.query().$promise,resource3.query().$promise])
  3. .then( function(result) {
  4. $scope.data1 = result[0];
  5. $scope.data2 = result[1];
  6. $scope.data3 = result[2];
  7.  
  8. console.log($scope.data1);
  9.  
  10. doSomething($scope.data1,$scope.data2);
  11. })
  12. }

我希望拯救别人一段时间。

猜你在找的Angularjs相关文章