我正在尝试将两个 HTTP.get 请求合并到一个 $scope
中,因为我想在同一个 ng-repeat
表中显示数据。我在我的 AngularJS 应用程序中使用这样的链式 promise :
AngularJS:
function getContainer() {
$http.get("/api/containers")
.then(function (response) {
$scope.GTMcontainersAccount = response.data;
$scope.loading = false;
// Retrive containerId and AccountID then request to show Counter of how many Tags, Triggers and Variables are available in each Container
angular.forEach($scope.GTMcontainersAccount, function (key) {
angular.forEach(key, function (keydata) {
$scope.accountId = keydata.accountId;
$scope.containerId = keydata.containerId;
$http.get("/api/tags", {
params: {
"param1": $scope.accountId,
"param2": $scope.containerId
}
}).then(function (responses) {
$scope.tags_counter = responses.data[""]['tags'];
$scope.trigger_counter = responses.data[""]['trigger'];
$scope.variable_counter = responses.data[""]['variable'];
})
})
})
})
}
解释一下我在代码中做了什么:
1) http.get(/api/containers)
给我一些 JSON 格式的数据
2) angular.forEach 通过键进行迭代
3) angular.forEach 迭代值(键是动态的,这就是为什么有 2 个 angular.forEach。
4) http.get(/api/tags)
我在参数中发送一些数据并获取一些结果数据
5) 这是困难的部分,我想在 $scope.GTMcontainersAccount
上使用 ng-repeat,但无法将 $scope.GTMcontainersAccount
与 $scope 合并.tags_counter
、$scope.trigger_counter
和 $scope.variable_counter
我在$scope.GTMcontainersAccount
中的数据:
{
"Account2": [{
"accountId": "1746756959",
"containerId": "7454209",
}, {
"accountId": "1746756959",
"containerId": "7519183",
}],
"Account 1": [{
"accountId": "1766143525",
"containerId": "7483653",
}]
}
我的数据在$scope.tags_counter:
(我删除了很多,因为它很长,这里重要的是最后一个键:值)
{
"0": {
"accountId": "****",
"containerId": "*****",
"tag": [{
"accountId": "*****",
"containerId": "********",
"name": "Test Tag"
}, {
"accountId": "***",
"containerId": "***",
"name": "Classic Google Analytics"
}, {
"accountId": "***",
"containerId": "***",
"name": "contentEngagement"
}],
"trigger": [{
"accountId": "***",
"containerId": "***",
"name": "Trigger 1"
}, {
"accountId": "***",
"containerId": "***",
"name": "Trigger 2"
}, {
"accountId": "***",
"containerId": "***",
"name": "Trigger 3"
}],
"variable": [{
"accountId": "***",
"containerId": "***",
"name": "Google Analytics Settings"
}, {
"name": "Protocol"
}, {
"accountId": "*******",
"containerId": "****",
"name": "Lookup Table"
}],
"builtInVariable": [{
"accountId": "*******",
"containerId": "******",
"name": "Page URL",
"path": null,
"type": "pageUrl",
"workspaceId": null
}, {
"accountId": "**",
"containerId": "**",
"name": "Page Hostname",
"path": null,
"type": "pageHostname",
"workspaceId": null
}, {
"accountId": "***",
"containerId": "***",
"name": "Page Path",
"path": null,
"type": "pagePath",
"workspaceId": null
}, {
"accountId": "***",
"containerId": "***",
"name": "Referrer",
"path": null,
"type": "referrer",
"workspaceId": null
}, {
"accountId": "***",
"containerId": "****",
"name": "Event",
"path": null,
"type": "event",
"workspaceId": null
}]
},
"": { <----- THIS is what i want to merge
"tags": 3,
"trigger": 3,
"variable": 3
}
}
因此,合并后我希望 $scope.GTMcontainersAccount
看起来像这样:
最终:
{
"Account2": [{
"accountId": "1746756959",
"containerId": "7454209",
}, {
"accountId": "1746756959",
"containerId": "7519183",
}],
"Account 1": [{
"accountId": "1766143525",
"containerId": "7483653",
}],
"Counter" : {
"tags": 3,
"trigger": 3,
"variable": 3
}
最佳答案
我给你带来了一把 fiddle 。
看起来有点乱,但是效果很好。
希望这对您有帮助。 :)
$scope.GTMcontainersAccount['Counter'] = Object.values($scope.tags_counter)[(Object.keys($scope.tags_counter).length-1)];
添加一些解释,
(Object.keys($scope.tags_counter).length-1)
上面是获取对象的最后一个索引。
Object.keys($scope.tags_counter)
返回 $scope.tags_counter
的所有 key
。
并且Object.values($scope.tags_counter)[index]
返回$scope.tags_counter
的所有值
,
因此,当您知道所需的值索引时,就可以轻松获取任何对象的值。
更新
你的照片让我很困惑。
为什么所有行都有相同的“计数器”对象。
您是否将“$scope.tags_counter”中的同一对象放入不同的对象中是值得怀疑的。
如果你想要获取的key总是空字符串,如""
,
然后您可以使用$scope.tags_counter[""];
。
或者你可以制作包含你的整个对象的 fiddle 吗?
.
.
更新2
在更加确定你的目的之后,我再次制作了这个 fiddle 。
如果这也不符合您的意图,请与我分享其他信息。
关于javascript - 合并来自两个http请求的AngularJS对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47569368/