Angular $scope.$digest vs $scope.$apply

Viewed 32653

I just want to know how to use $digest. Inside a controller the following code works fine and it updates the DOM after 3 seconds:

setTimeout(function(){
    $scope.$apply(function(){
        $scope.name = 'Alice';
    });
}, 3000);

However by using

setTimeout(function(){
        $scope.$digest(function(){
        $scope.name = 'Alice';
        });
    }, 3000);

nothing happens...

I thought that they do the same thing. Am I wrong?

4 Answers

A better understanding of $watch, $apply, and $digest is provided in this article.

Simply,
$watch that is used to detect changes in the UI
$apply would tell the $digest loop what changes should be applied
$digest loop would run and will ask every $watch for changes, the DOMs would change accordingly for what has what has been applied

You should call $apply only.

Related