【发布时间】:2018-05-14 03:23:05
【问题描述】:
我正在尝试将两个 HTTP.get 请求合并为一个 $scope,因为我想在同一个 ng-repeat 表中显示数据。我在我的 AngularJS 应用程序中使用这样的链式承诺:
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
}
【问题讨论】:
标签: javascript arrays angularjs merge