dimanche 19 avril 2015

How to update elements that are bound to functions with Angular?

What is the correct way to do have elements update regularly, without explicitly doing that for each variable in an $interval callback?


This would be useful in the following situation: an element's content is bound to a function, like this:



<p ng-bind="timeLeft()"></p>


The timeleft() function returns a value depending on the current time. It needs to be refreshed every 20 ms.


I've tried the following, which works:



$scope.updateView = function(){
if(!$scope.$$phase) { // To prevent this: "Error: [$rootScope:inprog] $digest already in progress"
$scope.apply();
}
}
$interval( function(){
$scope.updateView();
}, 50);


I noticed just using $interval may already suffice, in my setup this works too:



$scope.updateView = function(){
}
$interval( function(){
$scope.updateView();
}, 20);


Would it be reliable to use that last solution?


Aucun commentaire:

Enregistrer un commentaire