AngularJS摘要循环多久运行一次? [英] How often does the AngularJS digest loop run?

查看:127
本文介绍了AngularJS摘要循环多久运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论AngularJS的优点时,双向数据绑定通常被称为Angular对其他JS框架的主要优点。 深入挖掘,文档表明此过程是通过肮脏检查而不是通过事件驱动的措施来完成。首先,通过在每个周期内通过在后台进行方法触发,似乎在循环中检查所有的 $ watch es。然而,进一步阅读,似乎摘要循环实际上是由 rootScope.digest()触发的,而这又由 $触发。 ,这又是由一个事件(!)引发的,例如通过 ng-click 调用的onClick事件。

When discussing the merits of AngularJS, two-way data binding is often touted as a major benefit of Angular over other JS frameworks. Digging deeper, the documentation suggests this process is done through dirty-checking rather than through event-driven measures. At first, it seems that the digest-loop works by having a method fire off in the background at periodic intervals, checking all the $watches during each cycle. However, reading further, it seems that the digest-loop is actually triggered by rootScope.digest(), which in turn is triggered by $.apply, which is in turn triggered by an event(!), such as an onClick event called through ng-click.

但是,怎么可能?我以为Angular 不使用变更听众。那么摘要循环是如何运作的呢? Angular会自动在内部启动摘要循环,还是由事件触发摘要循环?如果摘要循环自动运行,运行的频率如何?

But, how can this be? I thought Angular doesn't use change listeners. So how does the digest-loop really operate? Does Angular automatically kick-off the digest-loop internally, or is the digest-loop triggered by events? If the digest-loop is run automatically, how often does it run?

一些澄清点:


  • 我不是询问手动绑定到更改时如何运行digest-loop。在这种情况下,如果要强制执行摘要循环,可以通过调用 $。apply()

  • m也不会询问摘要循环响应用户事件的频率。例如,如果ng-model在输入框上,Angular将在用户开始输入时启动摘要循环。令人困惑的部分是为了知道一个用户正在打字,Angular不会在某个地方使用一个基于事件的

  • 我已经知道有每个摘要循环最多10个循环。我的问题不在于每个摘要循环的循环次数,而是每秒钟运行的摘要循环的次数。

  • 奖金问题:如何使用摘要循环涉及JavaScript事件循环? JS事件循环是否在后台定期运行?消息循环与事件循环相同,但只能在角色环境中?这些完全不同的想法?

  • I'm not asking about how the digest-loop runs when manually binding to changes. In this case, if you want to force a digest loop, you can do so by calling $.apply()
  • I'm also not asking about how often digest loop runs in response to user events. For example, if ng-model is on an input box, Angular will kick-off a digest loop when a user starts typing. The confusing part is that in order to know a user was typing, doesn't Angular use an event-based onKeyUp somewhere?
  • I already know that there is a limit of 10 cycles max per digest-loop. My question is less about the number of cycles per digest-loop, but rather the number of digest-loops that run, say, per second.
  • Bonus questions: How does the digest-loop relate to the JavaScript event-loop? Does the JS event loop run periodically in the background? Is the digest-loop the same thing as the event loop, but only in the "Angular Context"? Are these totally different ideas?

推荐答案

角色摘要被触发 - 它们不会发生在

Angular digests are triggered - they don't happen through polling.

代码执行后,角色触发摘要。

Code executes, after code is done, angular triggers a digest.

示例:

 element.on('click', function() {
     $scope.$apply(function() { 
         // do some code here, after this, $digest cycle will be triggered
     });
 });

Angular还会在编译/链接阶段后触发$ digest:

Angular will also trigger a $digest after the compile/link phase:

Compile > Link > Digest

以及触发了多少个摘要循环?这取决于范围变量的稳定程度。通常需要至少2个周期来确定。

And as to how many digest cycles are triggered? It depends on how soon the scope variables stabalise. Usually it takes at least 2 cycles to determine that.

这篇关于AngularJS摘要循环多久运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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