top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to setup autoRefresh for AngularJS?

+1 vote
347 views
How to setup autoRefresh for AngularJS?
posted Jan 20, 2017 by Ramya

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You can use $interval(fuctionToRepeat, intervalInMillisecond) as documented here.

var myPr = angular.module('myPr',[]);

myPr.controller("TodosController", function ($scope,$http){

    $scope.reload = function () {
        $http.get('http://localhost:3000/api/todos').
            success(function (data) {
                $scope.todos = data.todos;
            });
    };
    $scope.reload();
    $interval($scope.reload, 5000);
});

Actually, the most Angular way to do that would be:

function MyCtrl($scope, $interval) {
  $scope.rand = 0;

  function update() {
    $scope.rand = Math.random() * 10;
  }

  $interval(update, 1000);
}
That's the Angular equivalent of setInterval()
answer Jan 22, 2017 by Amit Kumar Pandey
...