AngularJS. $ Autorefresh $ scope variable in html

How can I run my automatic variable in a $ scope object?

//controller setInterval(function(){$scope.rand=Math.random(10)},1000); //template {{rand}} 

Rand is not updating on my page. How to update a variable?

+6
source share
3 answers
 function MyCtrl($scope, $timeout) { $scope.rand = 0; (function update() { $timeout(update, 1000); $scope.rand = Math.random() * 10; }()); } 

demo: http://jsbin.com/udagop/1/

+10
source

In fact, the most angular way to do this would be:

 function MyCtrl($scope, $interval) { $scope.rand = 0; function update() { $scope.rand = Math.random() * 10; } $interval(update, 1000); } 

What is the equivalent of Angular setInterval ()

+6
source

You can do:

 //controller function UpdateCtrl($scope) { $scope.rand = 0; setInterval(function() { $scope.$apply(function() { $scope.rand = Math.random(10); }); }, 1000); } 

and

 //template <div ng-controller="UpdateCtrl"> {{rand}} </div> 
+3
source

All Articles