推迟$摘要(提高DOM事件)后angularjs手表执行 [英] Defer angularjs watch execution after $digest (raising DOM event)

查看:122
本文介绍了推迟$摘要(提高DOM事件)后angularjs手表执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有触发DOM事件的手表:

I have a watch that triggers a DOM event:

scope.$watch(function() { return controller.selected; }, function(selected) {
    if (selected) {
        $input.trigger('focus');
    }
});

问题是,我有,做了范围'专注'的处理程序。$适用

$input.bind('focus', function() {
    scope.$apply(function() { controller.focused = true; });
});

所以,当我的 $观看是在 $消化,因为它试图触发会导致错误解雇另一个 $摘要

So when my $watch is fired from inside a $digest it causes an error because it tries to trigger another $digest.

解决方法我是把触发器在 $超时

The workaround I have is to put the trigger in a $timeout.

scope.$watch(function() { return controller.selected; }, function(selected) {
    if (selected) {
        $timeout(function() { $input.trigger('focus'); });
    }
});

这个工程...为止。这是处理这一正确的方法是什么?我不知道这是否捕捉每一种情况下,想看看是否有一个角批准的方式有一块code延迟为消化后。

This works ... so far. Is this the proper way to handle this? I'm not sure if this catches every case and would like to see if there is an angular approved way to have a piece of code defer for after the digest.

谢谢!

推荐答案

$超时通常是什么用于运行(浏览器呈现后和)摘要周期之后的东西。

$timeout is normally what is used to run something after a digest cycle (and after the browser renders).

$超时将导致函数执行后执行另一个摘要周期。如果你的触发不影响任何角度,你可以在 invokeApply 参数设置为来避免运行其它消化周期。

$timeout will cause another digest cycle to be executed after the function is executed. If your trigger does not affect anything Angular, you can set the invokeApply argument to false to avoid running another digest cycle.

如果你希望你的回调运行的之前浏览器呈现:如果code使用排队 $ evalAsync 从指令的,它应该在DOM后运行已被角操纵,但浏览器呈现前。但是,如果code使用排队 $ evalAsync 从控制器的,之前的DOM已被角操纵,它将运行(与前浏览器呈现)。参见<一个href=\"http://stackoverflow.com/a/17303759/215945\">http://stackoverflow.com/a/17303759/215945.

If you want your callback to run before the browser renders: If code is queued using $evalAsync from a directive, it should run after the DOM has been manipulated by Angular, but before the browser renders. However, if code is queued using $evalAsync from a controller, it will run before the DOM has been manipulated by Angular (and before the browser renders). See also http://stackoverflow.com/a/17303759/215945.

这篇关于推迟$摘要(提高DOM事件)后angularjs手表执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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