【问题标题】:Merge two SCOPES from separate sources (same construct)合并来自不同来源的两个 SCOPES(相同的结构)
【发布时间】:2017-01-16 17:38:23
【问题描述】:

我有一个函数

var getData = function() {
    $http.get('(url of remote site)/swi/20?expand=true').success(function (data) {
        $scope.listOfServices = data.runningServices;
    });
};

每 30 秒更新一次并提取服务列表。

但是我想将其扩展为也可以从中提取

$http.get('(url of remote site)/**pad**/20?expand=true') 

和$

http.get('(url of remote site)/**bri**/20?expand=true') 

例如,然后将 rusults(所有格式完全相同)合并到一个 $scope

如何将这些 HTTP 获取与 URL 中的三个不同端点合并?

编辑:按照第一条评论中的链接,我想出了这个

var getData = function() {
        var swindon = $http.get('(url)/swi/10?expand=true'),
            paddington = $http.get('(url)/pad/10?expand=true'),
            reading = $http.get('(url)/rdg/10?expand=true');
        $q.all([swindon,paddington,reading]).then(function(arrayOfResults) {
            $scope.listOfServices = arrayOfResults;
            console.log($scope.listOfServices);
        });
    };

  getData();

但现在的问题是我有一个数组数组,我根本无法以任何方式访问。我什至在重复中重复了一遍

这是带有记录范围的 Google COnsole 的屏幕截图

我通常会遍历 listOfServices.trainServices

最终编辑:

知道了,在循环中正确使用循环,即

<tbody ng-repeat="item in listOfServices">
      <tr ng-repeat="service in item.data.trainServices">

【问题讨论】:

    标签: angularjs merge angularjs-scope remote-server


    【解决方案1】:

    您可以嵌套调用,如下所示:

    $scope.listOfServices = [];
    $http.get('(url of remote site)/swi/20?expand=true')
    .success(function (data, status, headers, config) {
      $scope.listOfServices.concat(data.runningServices);
    
      $http.get('(url of remote site)/pad/20?expand=true')
      .success(function (data, status, headers, config) {
        $scope.listOfServices.concat(data.runningServices);
    
        //nest Another and so on...
        //$http.get('(url of remote site)/bri/20?expand=true')..
      });
    
    })
    .error(function (data, status, headers, config) {$scope.listOfServices = [];});
    

    或使用已解决的承诺,如下面的答案:

    angular -- accessing data of multiple http calls - how to resolve the promises

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2016-12-09
      相关资源
      最近更新 更多