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?
function MyCtrl($scope, $timeout) { $scope.rand = 0; (function update() { $timeout(update, 1000); $scope.rand = Math.random() * 10; }()); }
demo: http://jsbin.com/udagop/1/
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 ()
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>