AngularJS中的$ evalAsync和$ timeout有什么区别? [英] What's the difference between $evalAsync and $timeout in AngularJS?

查看:68
本文介绍了AngularJS中的$ evalAsync和$ timeout有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用AngularJS了一段时间,发现需要使用 $ timeout 偶尔(似乎通常是初始化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).

最近,我一直在尝试对摘要循环进行更好,更深入的了解,并且遇到了

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

似乎该函数产生的结果与$timeout类似,只是您不给它延迟.每次使用$timeout时,都会延迟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.

推荐答案

我最近在这里基本上回答了这个问题: https://stackoverflow .com/a/17239084/215945 (该答案链接到与Misko进行的一些github交流.)

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

总结:

  • 如果代码使用指令中的 $ evalAsync 进行排队,则应该在已由Angular操作DOM之后之后运行,但之前浏览器渲染
  • 如果使用来自控制器的 $ evalAsync 将代码放入队列中,则应在被Angular操纵DOM之前(以及在浏览器呈现之前)运行它-很少这样做你想要这个
  • 如果代码使用 $ timeout 排队,则应在已由Angular处理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和$ timeout有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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