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

Angularjs multiple $http.get请求

发布时间:2020-12-17 08:39:28 所属栏目:安全 来源:网络整理
导读:我需要做两个$ http.get调用,我需要发送返回的响应数据到我的服务进行进一步的计算。 我想做一些如下: function productCalculationCtrl($scope,$http,MyService){ $scope.calculate = function(query){ $http.get('FIRSTRESTURL',{cache: false}).success
我需要做两个$ http.get调用,我需要发送返回的响应数据到我的服务进行进一步的计算。

我想做一些如下:

function productCalculationCtrl($scope,$http,MyService){
    $scope.calculate = function(query){

            $http.get('FIRSTRESTURL',{cache: false}).success(function(data){
                $scope.product_list_1 = data;
            });

            $http.get('SECONDRESTURL',{'cache': false}).success(function(data){
                $scope.product_list_2 = data;
            });
            $scope.results = MyService.doCalculation($scope.product_list_1,$scope.product_list_2);
        }
    }

在我的标记我叫它像

<button class="btn" ng-click="calculate(query)">Calculate</button>

由于$ http.get是异步的,我在传递doCalculation方法时没有获取数据。

任何想法如何实现多个$ http.get请求和工作像上面的实现传递响应数据服务?

提前致谢。

你需要的是$ q.all。

添加$ q到控制器的依赖项,然后尝试:

$scope.product_list_1 = $http.get('FIRSTRESTURL',{cache: false});
$scope.product_list_2 = $http.get('SECONDRESTURL',{'cache': false});

$q.all([$scope.product_list_1,$scope.product_list_2]).then(function(values) {
    $scope.results = MyService.doCalculation(values[0],values[1]);
});

(编辑:李大同)

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

    推荐文章
      热点阅读