So I have an array of object similar to this:
[ { id: 123, info: 'text', number: 0 }, ... ]
I have an API that takes the ID of the object and returns a number associated with it. On a front end app using JavaScript, I want to iterate over each object in the array, and populate that number property with data from this API.
I am using AngularJS and this is the function I wrote that fetches the number associated with each object:
$scope.getNumber = function(id) {
$http.get('/api/getnumber/' + id)
.success(function(data) {
console.log(data.goings);
return data.number;
});
}
this function works. My problem is I don't know how to update the number property in each object in the array. I've tried looping over it, but this doesn't work as the loop executes before .get()
is done fetching data. I've also tried in an html to have the following code:
<div ng-repeat="obj in data">
{{ getNumber(obj.id) || 0 }}
</div>
But this creates an infinite loop which crashes my browser. I tried doing some searching but I'm having trouble finding a comparable case. How can I achieve this?