What is about digest cycle in angular?

陌路散爱 提交于 2019-12-08 03:33:14

问题


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?

来源:https://stackoverflow.com/questions/33479752/what-is-about-digest-cycle-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!