You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adev/src/content/best-practices/runtime-performance/zone-pollution.md
+42-44Lines changed: 42 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,63 +21,63 @@ In the image above, there is a series of change detection calls triggered by eve
21
21
22
22
In such cases, you can instruct Angular to avoid calling change detection for tasks scheduled by a given piece of code using [NgZone](/api/core/NgZone).
23
23
24
-
<docs-codeheader="Run outside of the Zone"language='ts'linenums>
25
-
import { Component, NgZone, OnInit } from '@angular/core';
24
+
```ts {header:"Run outside of the Zone" , linenums}
The preceding snippet instructs Angular to call `setInterval` outside the Angular Zone and skip running change detection after `pollForUpdates` runs.
38
38
39
39
Third-party libraries commonly trigger unnecessary change detection cycles when their APIs are invoked within the Angular zone. This phenomenon particularly affects libraries that set up event listeners or initiate other tasks (such as timers, XHR requests, etc.). Avoid these extra cycles by calling library APIs outside the Angular zone:
40
40
41
-
<docs-codeheader="Move the plot initialization outside of the Zone"language='ts'linenums>
42
-
import { Component, NgZone, OnInit } from '@angular/core';
41
+
```ts {header:"Move the plot initialization outside of the Zone" , linenums}
Running `Plotly.newPlot('chart', data);` within `runOutsideAngular` instructs the framework that it shouldn’t run change detection after the execution of tasks scheduled by the initialization logic.
58
58
59
59
For example, if `Plotly.newPlot('chart', data)` adds event listeners to a DOM element, Angular does not run change detection after the execution of their handlers.
60
60
61
61
But sometimes, you may need to listen to events dispatched by third-party APIs. In such cases, it's important to remember that those event listeners will also execute outside of the Angular zone if the initialization logic was done there:
62
62
63
-
<docs-codeheader="Check whether the handler is called outside of the Zone"language='ts'linenums>
64
-
import { Component, NgZone, OnInit, output } from '@angular/core';
63
+
```ts {header:"Check whether the handler is called outside of the Zone" , linenums}
If you need to dispatch events to parent components and execute specific view update logic, you should consider re-entering the Angular zone to instruct the framework to run change detection or run change detection manually:
95
94
96
-
<docs-codeheader="Re-enter the Angular zone when dispatching event"language='ts'linenums>
97
-
import { Component, NgZone, OnInit, output } from '@angular/core';
95
+
```ts {header:"Re-enter the Angular zone when dispatching event" , linenums}
The scenario of dispatching events outside of the Angular zone may also arise. It's important to remember that triggering change detection (for example, manually) may result in the creation/update of views outside of the Angular zone.
0 commit comments