AngularJS:$ evalAsync VS $超时 [英] AngularJS : $evalAsync vs $timeout

查看:167
本文介绍了AngularJS:$ evalAsync VS $超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用AngularJS一小会儿,现在,已经发现需要使用 $超时在每过一段时间(似乎通常是初始化一个jQuery插件)。

I've been using AngularJS for a little while now, and have found the need to use $timeout every once in a while (Seems to usually be to init a jQuery plugin).

最近,我一直在试图让消化周期的更好,更深入的了解,而且我碰到的 $ evalAsync 功能。

Recently, I've been trying to get a better and more in-depth understanding of the digest cycle, and I came across $evalAsync function.

这似乎是这个函数产生类似的结果为 $超时,只有你不给它耽误。我每次使用 $超时已经以0延迟,所以现在我想知道如果我应该用 $ evalAsync 代替。

It seems like that function produces similar results to $timeout, only you don't give it delay. Every time I've used $timeout it has been with a delay of 0, so now I'm wondering if I should have used $evalAsync instead.

有没有两者之间有什么根本的区别?你会用什么情况下,一个比其他?我想获得的时候使用哪一种更好的感觉。

Are there any fundamental differences between the two? What cases would you use one over the other? I'd like to get a better feeling of when to use which one.

推荐答案

最近,我在这里基本上回答了这个问题:的http://计算器.COM / A /二十一万五千九百四十五分之一千七百二十三万九千零八十四
(这个答案的链接与一些MISKO GitHub的交流。)

I recently answered essentially this question here: http://stackoverflow.com/a/17239084/215945 (That answer links to some github exchanges with Misko.)

要总结一下:


  • 如果code采用排队的 $ evalAsync从指令,它应该运行的之后的DOM中已被角操纵的,但是的浏览器呈现

  • 如果code采用排队的 $ evalAsync从控制器,它应该运行的的DOM中已被角操纵(和浏览器呈现前) - - 很少做你想做这个

  • 如果code使用的 $超时排队,它应该运行的之后的DOM中已被角操纵和的之后的了浏览器呈现(这可能会导致在某些情况下的闪烁)

  • 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
  • if code is queued using $evalAsync from a controller, it should run before the DOM has been manipulated by Angular (and before the browser renders) -- rarely do you want this
  • if code is queued using $timeout, it should run after the DOM has been manipulated by Angular, and after the browser renders (which may cause flicker in some cases)

这篇关于AngularJS:$ evalAsync VS $超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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