I'm trying to merge two HTTP.get request into one $scope because i want to display the data in same ng-repeat table. Im using chained promises like this in my AngularJS application:
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'];
})
})
})
})
}
Explain what i do in the code:
1) http.get(/api/containers) gives me some data in JSON-format
2) angular.forEach iterates trough the keys
3) angular.forEach iterates trough the value (key are dynamic thats why there are 2 angular.forEach.
4) http.get(/api/tags) i send some data in parameter and get some result data
5) Here is the difficult part, i want to use ng-repeat on $scope.GTMcontainersAccount but cannot merge $scope.GTMcontainersAccount with $scope.tags_counter, $scope.trigger_counter and $scope.variable_counter
My data in $scope.GTMcontainersAccount:
{
"Account2": [{
"accountId": "1746756959",
"containerId": "7454209",
}, {
"accountId": "1746756959",
"containerId": "7519183",
}],
"Account 1": [{
"accountId": "1766143525",
"containerId": "7483653",
}]
}
My data in $scope.tags_counter: (ive deleted alot because its very long and whats important here is the LAST key:value)
{
"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
}
}
So after the merge i want $scope.GTMcontainersAccount to look like this:
Final:
{
"Account2": [{
"accountId": "1746756959",
"containerId": "7454209",
}, {
"accountId": "1746756959",
"containerId": "7519183",
}],
"Account 1": [{
"accountId": "1766143525",
"containerId": "7483653",
}],
"Counter" : {
"tags": 3,
"trigger": 3,
"variable": 3
}
