如何AngularJS的$手表功能的工作? [英] How does AngularJS's $watch function work?

查看:174
本文介绍了如何AngularJS的$手表功能的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多关于AngularJS现在,我穿过那个神奇的手表$功能来了。我知道如何使用它,但我不知道它是如何在后台执行。它是一个时间间隔功能?或者是角调用此表中的每条语句执行的?

I'm reading a lot about AngularJS nowadays, and I came across that magical $watch function. I understand how to use it, but I wonder how is it implemented in the background. Is it a time interval function? Or is it Angular calling this watch every statement executed?

我不想现在就深入到源$ C ​​$ C,我会很高兴,如果你们中的一个已经知道答案,并希望分享他对学科知识。

I don't want to dig into the source code right now, and I would glad if one of you already know the answer and want to share his knowledge about the subject.

感谢。

推荐答案

所有的手表都评估(有时是多次)每消化循环。摘要循环进入一些事件的结果,或调用$适用()。手表不叫周期性基于一个计时器。

All watches are evaluated (sometimes multiple times) every digest loop. The digest loop is entered as a result of some event, or calling $apply(). Watches are not called periodically based on a timer.

请参阅 http://docs.angularjs.org/guide/concepts

有关事件浏览器的事件循环等待到达。该事件的回调会被执行。一旦回调执行,浏览器离开了JavaScript的背景下,重新呈现基于DOM的看法改变了。角修改由提供它自己的事件处理循环正常的JavaScript流程。这种分裂的JavaScript到古典和角执行上下文...角执行时(有些)stimulusFn(),它通常会修改应用程序状态。角进入$消化循环。循环是由两个小环,其过程$ evalAsync队列和$观察名单中。在$消化循环迭代保持,直至模型稳定,这意味着$ evalAsync队列为空,$观察名单没有检测到任何变化。在$观察名单是一组前pressions的它自最后一次迭代可能已经改变。如果检测到变化则$手表功能被调用其通常更新与新的值在DOM

The browser's event-loop waits for an event to arrive. The event's callback gets executed... Once the callback executes, the browser leaves the JavaScript context and re-renders the view based on DOM changes. Angular modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and Angular execution context... Angular executes [some] stimulusFn(), which typically modifies application state. Angular enters the $digest loop. The loop is made up of two smaller loops which process $evalAsync queue and the $watch list. The $digest loop keeps iterating until the model stabilizes, which means that the $evalAsync queue is empty and the $watch list does not detect any changes. The $watch list is a set of expressions which may have changed since last iteration. If a change is detected then the $watch function is called which typically updates the DOM with the new value.

这篇关于如何AngularJS的$手表功能的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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