合并来自不同来源的两个 SCOPES(相同的结构)
Merge two SCOPES from separate sources (same construct)
我有一个功能
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 中的三个不同端点合并?
编辑:根据第一条评论中的 link,我想出了这个
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 控制台的屏幕截图,其中包含已记录的范围
我通常循环 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 = [];});
或使用已解决的承诺,如以下答案所示:
我有一个功能
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 中的三个不同端点合并?
编辑:根据第一条评论中的 link,我想出了这个
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 控制台的屏幕截图,其中包含已记录的范围
我通常循环 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 = [];});
或使用已解决的承诺,如以下答案所示: