mosh442 mosh442 - 1 year ago 87
AngularJS Question

AngularJS ngcontroller to be reloading data periodically

I have this working implementation af a angularJS app which fetches some links from an URL and paint them.
However the links on the URL are being updated constantly, I would like to update periodcially this $scope.listlinks, let say every 10 seconds.

I tried playing with setInterval with no luck.


var App = angular.module('App', []);
function get($scope, $http ) {
function(res){$scope.listlinks =;});


<div id="cnt1" ng-controller="ListLinksCtrl">
<div ng-repeat="singlelink in listlinks">
<a href="{{ singlelink.url }}">
{{ singlelink.destination }}

Answer Source

While jvandemo's answer will work, I think it can be improved slightly. By using setInterval, it breaks the dependency injection convention that Angular follows and makes unit testing of the controller difficult.

Angular doesn't currently support setInterval through its built-in services, but you can use the $timeout service to produce the same functionality. I'd change the controller to this:

app.controller('MainCtrl', function($scope, $http, $timeout) {

  // Function to get the data
  $scope.getData = function(){
      .success(function(data, status, headers, config) {

      // Your code here
      console.log('Fetched data!');

  // Function to replicate setInterval using $timeout service.
  $scope.intervalFunction = function(){
    $timeout(function() {
    }, 1000)

  // Kick off the interval

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download