I'm trying to assign array elements in a for loop with asynchronous results of a call to a ngResource
action.
for ( var i = 0; i < projs.length; i++) {
$scope.projets[i].redacteur = new Object(); // the Object where the result will be stored
var param = new Object();
param.email = projs[i].redacteurEmail;
Agent.read(param, function(data) {
$scope.projets[i].redacteur = data;
});
}
The problem is : when the callback function is executed (when the data is received), i
is out of bounds (it passed the last i++
). Then the data received is assigned to a nonexistent object.
Any idea of a solution to this issue ?