加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 角度资源调用和$q

发布时间:2020-12-17 08:18:26 所属栏目:安全 来源:网络整理
导读:伙计们, 我的代码设置有些如下: $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(data
伙计们,

我的代码设置有些如下:

$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); 
                 }
}

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

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

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

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

$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); 
           })
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读