Angular:摘要事件完成并更新视图 [英] Angular: event for digest completed and view updated

查看:28
本文介绍了Angular:摘要事件完成并更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图更新时是否有监听事件?是否与已完成的摘要有关,我应该听这个以及如何听?例如有一个视图在 div itemDetail 中输出:

{{titleOriginal}}

和相应的控制器:

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

现在此警报将抛出 {{titleOriginal}} 而不是评估值...".所以我想我应该首先等待视图更新然后警报.如何?什么时候?请帮忙.

解决方案

您可以使用 $timeout ,它不会在摘要循环完成之前运行.

$timeout(function(){//执行此代码时应更新});

$timeoutwindow.setTimeoutset 的 Angular 包装器.超时将函数从执行队列中移除,并且只有在 JavaScript 完成当前执行队列后才会调用它.因此,您的模型将在执行前更新.

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.

解决方案

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.

这篇关于Angular:摘要事件完成并更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆