我认为在angular2中使用zonejs的主要目的是知道何时渲染。
根据NgZone Primer(第5章:用例/用例3:框架自动渲染)
Frameworks,such as Angular,need to know when all of the application
@H_502_17@
work has completed and perform DOM update before the host environment
performs the pixel rendering. In practice this means that the
framework is interested when the main task and the associated micro
tasks have executed but before the VM hands over the control to the
host.Angular使用区域来修补异步API(addEventListener,setTimeout(),…),并使用来自这些修补API的通知在每次发生某些异步事件时运行更改检测。
对于这个Angular区域有onMicrotaskEmpty事件
https://github.com/angular/angular/blob/2.2.4/modules/%40angular/core/src/zone/ng_zone.ts#L199
和ApplicationRef订阅此事件以触发更改检测(Application.tick)
zonejs也可用于调试,测试和分析。如果您遇到一些错误,它可以帮助您查看整个调用堆栈。
区域补丁异步API如:
Promise XHR fetch Error addEventListener removeEventListener FileReader WebSocket MutationObserver WebKitMutationObserver document.registerElement navigator.geolocation.getCurrentPosition navigator.geolocation.watchPosition copy cut paste abort blur focus canplay canplaythrough change click contextmenu dblclick drag dragend dragenter dragleave dragover dragstart drop durationchange emptied ended input invalid keydown keypress keyup load loadeddata loadedMetadata loadstart message mousedown mouseenter mouseleave mousemove mouSEOut mouSEOver mouseup pause play playing progress ratechange reset scroll seeked seeking select show stalled submit suspend timeupdate volumechange waiting mozfullscreenchange mozfullscreenerror mozpointerlockchange mozpointerlockerror error webglcontextrestored webglcontextlost webglcontextcreationerror setTimeout/clearTimeout setInterval/clearInterval setImmediate/clearImmediate requestAnimationFrame/cancelAnimationFrame mozRequestAnimationFrame/mozCancelAnimationFrame webkitRequestAnimationFrame/webkitCancelAnimationFrame alert prompt confirm这篇文章可能有助于理解它在angular2中的工作原理
> http://blog.thoughtram.io/angular/2016/02/01/zones-in-angular-2.html
> http://blog.kwintenp.com/how-the-hell-do-zones-really-work/