问题描述
我一直在使用AngularJS一小会儿,现在,已经发现需要使用功能。
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.
推荐答案
最近,我在这里基本上回答了这个问题:
(这个答案的链接与一些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 $超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!