The Plunkr
Check out the Plunkr here!
Here's the HTML code:
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@*" data-semver="1.2.15" src="http://code.angularjs.org/1.2.15/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="myApp" ng-controller="MyCtrl">
{{data.theDate}}
</body>
</html>
And here's the JavaScript code:
angular.module("myApp", []).service("UpdatingService", function($http, $timeout){
var service = {
data:{},
getData:function(){
$http.get("someData.json").success(function(result){
angular.copy(result, service.data)
service.data.theDate+=" "+new Date();
});
}
};
cancelRefresh = $timeout(function myFunction() {
service.getData();
cancelRefresh = $timeout(myFunction, 1000);
},1000);
return service;
}).controller("MyCtrl", function($scope, UpdatingService){
$scope.data = UpdatingService.data;
});
As shown above, create a service for handling requests and storing data. Then create a controller to utilize this service. The service uses AngularJS features like $http and $timeout to make AJAX requests and continuously poll the server for updates. To stop the timer, refer to the link below for more details.
More information on using setInterval in AngularJS factory