I can not figure out how to make a dynamic data update. Suppose there is an array of users objects, and I want to receive data about these users once a second using $http.get() . If it were not for the asynchrony, it would look like this:
var users = []; setInterval(function () { for (var i = 0; i < users.length; i++) { $http.get('/user/'+i).then(function (res) { users[i] = res.data; }); } }, 1000); But when asynchrony comes into play, i naturally no longer equal to what is needed. To make the users[i].update(data) function that would also assign new data through the this object, the context of this is also lost (it becomes equal to the window )
UPDATE Found a solution, but it looks more like a crutch:
var users = []; setInterval(function () { for (var i = 0; i < users.length; i++) { $http.get('/user/'+i).success((function (object) { return function (data) { for (var key in data) { object[key] = data[key]; } } })(users[i])); } }, 1000);
i- it will look better :) - Grundy