What is about digest cycle in angular?

2019-06-02 03:00发布

问题:

Let we have the code in the control's method executed when, for example, we push a button

$timeout(function() {
    $scope.names = ['A', 'B', 'C'];
}, 0, false).then(function() {
    $scope.names.push('D');
})

in this case we will not see any changes at the screen because the digest cycle will not be running

But if we wrote this

$timeout(function() {
    $scope.names = ['A', 'B', 'C'];
}, 0, false).then(function() {
    $scope.names.push('D');

    return $q.when();
})

we will see changes because when we return promise the $Q provider decides to run the digest cycle. http://plnkr.co/edit/DqAzPBe37Qd9CU1ug8Pp?p=preview

Could you please explain me why, I don't understand the logic of this behavior. How to prevent this?