By calling $scope.$apply()
, you can initiate a digest cycle which internally invokes $digest
. The following example demonstrates manual changes:
The variable number
will not be bound as the timeout function takes it out of Angular's scope.
setTimeout(function () {
$scope.number = Math.random();
});
However, you can "force" it to display by applying scope changes manually:
setInterval(function () {
$scope.$apply();
}, 100);
Demos:
No change / Change with manual updates
This method will not activate watchers. In the $digest
implementation, it verifies if the value has changed since the previous watch evaluation and triggers the callback only if there has been a change.
if ((value = watch.get(current)) !== (last = watch.last) ... [rootScope.js]
Hence, you must somehow modify the value of the last execution, which is achievable through the $$watchers
object on the scope:
$scope.digest = function () {
setTimeout(function () {
angular.forEach($scope.$$watchers, function (w) {
w.last.value = Math.random();
});
$scope.$apply();
});
}
DEMO