Angular: event for digest completed and view updated

ぐ巨炮叔叔 提交于 2019-12-24 12:59:15

问题


is there an event to listen when the view is updated? Is it related to digest completed, should I listen to this and how? For example there is a view that outputs in div itemDetail:

<div id="itemDetail">{{titleOriginal}}</div>

and corresponding controller:

.controller('ItemDetailController', function($scope) {
    $scope.titleOriginal = "...";
    alert(angular.element("#itemDetail").html());
})

Now this alert would throw {{titleOriginal}} instead of evaluated value "...". So I guess I should first wait for view to be updated and then alert. How? When? Please help.


回答1:


You could use $timeout which will not be run until digest cycle completes.

$timeout(function(){
   // should be updated when this code is executed
});

$timeout is an Angular's wrapper for window.setTimeoutset. Timeout removes the function from the execution queue and it will only be invoked after JavaScript has finished with the current execution queue. So that is way your model will be updated before the execution.



来源:https://stackoverflow.com/questions/33758662/angular-event-for-digest-completed-and-view-updated

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