$interval
service within your controller and periodically refresh the data in your ViewModel.
For example, the following controller will initially load the list of Users
known to the system, and then every 3 seconds refresh that list making it available
through the controller:
app.controller('UserListController', [ '$http', '$scope', '$interval', 'authentication',
function UserListController($http, $scope, $interval, authentication) {
var vm = this;
vm.pageHeader = {
title: 'User List'
};
// Initially gets list of users
getAllUsers($http)
.success(function(data) {
vm.users = data;
vm.message = "Users list found!";
})
.error(function (e) {
vm.message = "Could not get list of users";
});
// Refreshes lists of users periodically
$scope.callAtInterval = function() {
console.log("Interval occurred");
getAllUsers($http)
.success(function(data) {
vm.users = data;
vm.message = "Users list found!";
})
.error(function (e) {
vm.message = "Could not get list of users";
});
}
$interval( function(){$scope.callAtInterval();}, 3000, 0, true);
}]);
vm.users
array of objects will automatically be updated when the interval occurs and
there is new or changed data.$interval
is
here.