Sharing Top Content from the Angular-sphere.

Do you still think that NgZone (zone.js) is required for change detection in Angular?

  • Angular implements NgZone class that relies heavily on the tasks tracking mechanism.NgZone is just a wrapper around a forked child zone:function NgZonePrivate) { zone.
  • _inner.fork({ name: ‘angular’, …The forked zone is kept in the _inner property and is usually referred to as Angular zone.
  • _inner.run(fn, applyThis, applyArgs);}The current zone at the moment of forking the Angular zone is kept in the _outer property and is used to run a callback when you execute { return this.
  • _outer.run(fn);}This method is often used to run performance heavy operations outside Angular zone to avoid constantly triggering change detection.NgZone has isStable property that denotes whether there are no outstanding micro or macro tasks.
  • It also defines four Event | Description onUnstable | Notifies when code enters Angular Zone.

Most articles I have seen strongly associate Zone(zone.js) and NgZone with change detection in Angular. And although they are definitely related, technically they are not part of one whole. Yes, Zone…

How NgZone uses ZonesIn my previous article on Zone (zone.js) I described in depth the inner working and API that Zone provides. There I explained the core concepts of forking a zone and running a task in a particular zone. I’ll be referring to those concepts here.I also demonstrated two capabilities that Zone provides — context propagation and outstanding asynchronous tasks tracking. Angular implements NgZone class that relies heavily on the tasks tracking mechanism.NgZone is just a wrapper around a forked child zone:function NgZonePrivate) { zone._inner = zone._inner.fork({ name: ‘angular’, …The forked zone is kept in the _inner property and is usually referred to as Angular zone. This is the zone that is used to run a callback when you execute NgZone.run():run(fn, applyThis, applyArgs) { return this._inner.run(fn, applyThis, applyArgs);}The current zone at the moment of forking the Angular zone is kept in the _outer property and is used to run a callback when you execute { return this._outer.run(fn);}This method is often used to run performance heavy operations outside Angular zone to avoid constantly triggering change detection.NgZone has isStable property that denotes whether there are no outstanding micro or macro tasks. It also defines four Event | Description onUnstable | Notifies when code enters Angular Zone. || | This gets fired first on VM Turn. || | || onMicrotaskEmpty | Notifies when there is no more microtasks || | enqueued in the current VM Turn. || | This is a hint for Angular to do change || | detection which may enqueue more microtasks….

Do you still think that NgZone (zone.js) is required for change detection in Angular?

Comments are closed, but trackbacks and pingbacks are open.