10 Things Every Angular Developer Should Know About Zone.js

Every developer should know the basics about Zone.js

Matthias Junker
Oct 8, 2019 · 6 min read
Image for post
Image for post
Photo by Anton Scherbakov on Unsplash

1. Why Should I Care About Zone.js?

2. In a Nutshell: How Does Zone.js Work?

Zone

Tasks

NgZone

Root Zone/Forks

ZoneSpecs

3. You Don’t Have to Use Zone.js With Angular (but You Probably Should)

4. Automagic Change Detection Is Fine, but What If I Want Control?

constructor(private ngZone: NgZone) {  this.ngZone.runOutsideAngular(() => {
// this will not trigger change detection
setInterval(() => doSomething(), 100)
});
}

5. How Does Zone.js Affect Protractor Tests?

6. How Does RxJS Work With Zone.js?

import ‘zone.js/dist/zone-patch-rxjs’;

7. How to Exclude Certain Events From Zone.js

8. How Can I Analyze Problems With Zone.js?

// increase stack trace length (optional)
Error.stackTraceLimit = 100;
const longStackTraceZoneSpec = Zone.longStackTraceZoneSpec;
// increase stack trace limit (optional)
longStackTraceZoneSpec.longStackTraceLimit = 1000;
Zone.current
.fork(new Zone.TaskTrackingZoneSpec())
.fork(longStackTraceZoneSpec)
.run(() => {
// NgZone forks currentZone, which is here longStackTraceZone
const ngZone = new NgZone({enableLongStackTrace: true});
platformBrowserDynamic()
.bootstrapModule(AppModule, {ngZone: ngZone});
});
let macroTasks = Zone.current
// get the TaskTrackingZone via it's name
.get(‘TaskTrackingZone’)
.getTasksFor(‘macroTask')

9. What Are Micro and Macro Tasks?

10. How Does Zone.js Work With Ng-Upgrade?

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store