AngularJS如何知道变量何时更改? AngularJS脏检查如何工作? [英] How does AngularJS know when variables change? How does AngularJS dirty checking work?

查看:83
本文介绍了AngularJS如何知道变量何时更改? AngularJS脏检查如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些文章,以进一步了解AngularJS的工作原理.

I was reading some article to understand a little bit more how AngularJS works.

我不理解的术语之一是Dirty Checking.

One of the terms that I didn't understand is Dirty Checking.

到底是什么?看起来像观察者模式,但显然更好.

What is it exactly? It seems like the Observer pattern but apparently it's better.

您能帮我理解吗?

对于想了解更多有关此内容的人来说,观看此视频也很有用来自NgEurope几年前的 swiip .

EDIT : it can be also useful for people who wants to learn more about that to watch this video from swiip at NgEurope some years ago.

推荐答案

来自

Angular定义了所谓摘要的概念 循环.这个周期可以看作是一个循环,在此期间Angular 检查所有人监视的所有变量是否有任何更改 $ scopes.因此,如果您在控制器中定义了$ scope.myVar 并且此变量被标记为正在监视,那么您就是 明确地告诉Angular监视每个myVar上的更改 循环的迭代.

Angular defines a concept of a so called digest cycle. This cycle can be considered as a loop, during which Angular checks if there are any changes to all the variables watched by all the $scopes. So if you have $scope.myVar defined in your controller and this variable was marked for being watched, then you are explicitly telling Angular to monitor the changes on myVar in each iteration of the loop.

此"digest"也称为脏污检查",因为它以某种方式扫描更改范围.我不能说这是比可观察的模式好还是坏.这取决于您的需求.

This "digest" is also called "dirty checking", because, in a way, it scans the scope for changes. I cannot say if it's for better or for worse than observable pattern. It depends on your needs.

某些链接:

  • Angular documentation
  • A blog post about Angular scopes

这篇关于AngularJS如何知道变量何时更改? AngularJS脏检查如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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