我有一个功能
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')
例如,然后将合并结果(所有格式完全相同)合并为一个$ 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">
您可以嵌套调用,如下所示:
$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 = [];});
或使用已解决的诺言,如以下答案所示:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句