Skip to content

Commit 3adb091

Browse files
SkyZeroZxalxhub
authored andcommitted
docs: update zone pollution guide highlight syntax
1 parent 5be3304 commit 3adb091

1 file changed

Lines changed: 42 additions & 44 deletions

File tree

adev/src/content/best-practices/runtime-performance/zone-pollution.md

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,63 @@ In the image above, there is a series of change detection calls triggered by eve
2121

2222
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).
2323

24-
<docs-code header="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}
25+
import { Component, NgZone, OnInit, inject } from '@angular/core';
2626

2727
@Component(...)
2828
class AppComponent implements OnInit {
29-
private ngZone = inject(NgZone);
29+
private ngZone = inject(NgZone);
3030

31-
ngOnInit() {
32-
this.ngZone.runOutsideAngular(() => setInterval(pollForUpdates), 500);
31+
ngOnInit() {
32+
this.ngZone.runOutsideAngular(() => setInterval(pollForUpdates, 500));
33+
}
3334
}
34-
}
35-
</docs-code>
35+
```
3636

3737
The preceding snippet instructs Angular to call `setInterval` outside the Angular Zone and skip running change detection after `pollForUpdates` runs.
3838

3939
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:
4040

41-
<docs-code header="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}
42+
import { Component, NgZone, OnInit, inject } from '@angular/core';
4343
import * as Plotly from 'plotly.js-dist-min';
4444

4545
@Component(...)
4646
class AppComponent implements OnInit {
47-
private ngZone = inject(NgZone);
47+
private ngZone = inject(NgZone);
4848

49-
ngOnInit() {
50-
this.ngZone.runOutsideAngular(() => {
51-
Plotly.newPlot('chart', data);
52-
});
53-
}
49+
ngOnInit() {
50+
this.ngZone.runOutsideAngular(() => {
51+
Plotly.newPlot('chart', data);
52+
});
53+
}
5454
}
55-
</docs-code>
55+
```
5656

5757
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.
5858

5959
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.
6060

6161
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:
6262

63-
<docs-code header="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}
64+
import { Component, NgZone, OnInit, output, inject } from '@angular/core';
6565
import * as Plotly from 'plotly.js-dist-min';
6666

6767
@Component(...)
6868
class AppComponent implements OnInit {
69-
private ngZone = inject(NgZone);
69+
private ngZone = inject(NgZone);
7070

71-
plotlyClick = output<Plotly.PlotMouseEvent>();
71+
plotlyClick = output<Plotly.PlotMouseEvent>();
7272

73-
ngOnInit() {
74-
this.ngZone.runOutsideAngular(() => {
75-
this.createPlotly();
76-
});
77-
}
73+
ngOnInit() {
74+
this.ngZone.runOutsideAngular(() => {
75+
this.createPlotly();
76+
});
77+
}
7878

79-
private async createPlotly() {
80-
const plotly = await Plotly.newPlot('chart', data);
79+
private async createPlotly() {
80+
const plotly = await Plotly.newPlot('chart', data);
8181

8282
plotly.on('plotly_click', (event: Plotly.PlotMouseEvent) => {
8383
// This handler will be called outside of the Angular zone because
@@ -86,40 +86,38 @@ const plotly = await Plotly.newPlot('chart', data);
8686
console.log(NgZone.isInAngularZone());
8787
this.plotlyClick.emit(event);
8888
});
89-
89+
}
9090
}
91-
}
92-
</docs-code>
91+
```
9392

9493
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:
9594

96-
<docs-code header="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}
96+
import { Component, NgZone, OnInit, output, inject } from '@angular/core';
9897
import * as Plotly from 'plotly.js-dist-min';
9998

10099
@Component(...)
101100
class AppComponent implements OnInit {
102-
private ngZone = inject(NgZone);
101+
private ngZone = inject(NgZone);
103102

104-
plotlyClick = output<Plotly.PlotMouseEvent>();
103+
plotlyClick = output<Plotly.PlotMouseEvent>();
105104

106-
ngOnInit() {
107-
this.ngZone.runOutsideAngular(() => {
108-
this.createPlotly();
109-
});
110-
}
105+
ngOnInit() {
106+
this.ngZone.runOutsideAngular(() => {
107+
this.createPlotly();
108+
});
109+
}
111110

112-
private async createPlotly() {
113-
const plotly = await Plotly.newPlot('chart', data);
111+
private async createPlotly() {
112+
const plotly = await Plotly.newPlot('chart', data);
114113

115114
plotly.on('plotly_click', (event: Plotly.PlotMouseEvent) => {
116115
this.ngZone.run(() => {
117116
this.plotlyClick.emit(event);
118117
});
119118
});
120-
121-
}
119+
}
122120
}
123-
</docs-code>
121+
```
124122

125123
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

Comments
 (0)