简体   繁体   English

优化的数据绑定机制如何在Angular2中工作

[英]How does optimised data binding mechanism work in Angular2

I know data binding concept in AngularJs is a naive dirty checking mechanism. 我知道AngularJs中的数据绑定概念是一种幼稚的脏检查机制。 In the Google I/O videos , they have said that, they optimized the data binding/change detection in Angular2. 他们在Google I / O视频中说过,他们优化了Angular2中的数据绑定/更改检测。 How does it work in Angular2 它在Angular2中如何工作

Application state change can be caused by: 应用程序状态更改可能由以下原因引起:

  • Events, 活动,
  • XHR - Fetching data, XHR-提取数据,
  • Timers - setTimeout(), setInterval() 计时器-setTimeout(),setInterval()

They are all asynchronous. 它们都是异步的。

So how Angular 2 know, when to run change detector? 那么Angular 2怎么知道何时运行变化检测器? Because of Zones . 因为地带 There you can read more about it: 在那里,您可以了解更多信息:

http://blog.thoughtram.io/angular/2016/02/01/zones-in-angular-2.html http://blog.thoughtram.io/angular/2016/01/22/understanding-zones.html http://blog.thoughtram.io/angular/2016/02/01/zones-in-angular-2.html http://blog.thoughtram.io/angular/2016/01/22/understanding-zones.html

Zones have lifecycle events. Zones具有生命周期事件。 ApplicationRef is listening zones onTurnDone event. ApplicationRefonTurnDone事件的侦听区域。 Whenever this event is fired, it executes a tick() function which start change detection . 每当触发此事件时,它都会执行tick()函数,该函数启动更改检测

All components in Angular 2 have their own Change Detector (so it looks like component tree - from top to bottom). Angular 2中的所有组件都有自己的变更检测器 (因此它看起来像组件树-从上到下)。 Change Detector is comparing variables references and if references do not changed, CD checks properties (if variable is an object). 变更检测器正在比较变量引用,如果引用未更改,CD会检查属性(如果变量是对象)。

There is really awesome article about that: 真的有很棒的文章:

http://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html http://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM