问题
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