Given the following data on the server:
people.json:
{
"Bill": "Bill the Man",
"Jane": "Jane Doe",
"Jack": "Jack the Ripper"
}
groups.json:
[
["Bill", "Jack"],
["Jane", "Jack"]
]
I import it in controllers.js:
var websiteApp = angular.module('websiteApp', []);
websiteApp.controller('websiteCtrl', function ($scope, $http) {
// import data
$http.get('people.json').success(function (data) {
$scope.people = data;
});
$http.get('groups.json').success(function (data) {
$scope.groups = data;
});
// fill groups with people data
$scope.groups.forEach(function (group, i) {
group.forEach(function (person, j) {
$scope.groups[i][j] = $scope.people[person];
});
});
});
And then I use this with the following template:
<ul>
<li ng-repeat="group in groups">
<ul><li ng-repeat="person in group">{{person}}</li></ul>
</li>
</ul>
I expected to get a list of lists of names from people.json, but I get the first names as in groups.json. Some further investigation with console.log made it clear that the forEach loops are not executed. Further trials with local variables to store the data before assigning to the scope also did not succeed.
How should I approach this issue? What am I not understanding about how Angular works? (N.B.: I am new to Angular and Javascript frameworks in general.)
$http.getcalls are async. So, there will be no data yet when theforEachruns.foreachis being hit before the.successof eachhttpcall due to the async nature