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/skipping-subtrees.md
+3-16Lines changed: 3 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,33 +4,20 @@ JavaScript, by default, uses mutable data structures that you can reference from
4
4
5
5
Change detection is sufficiently fast for most applications. However, when an application has an especially large component tree, running change detection across the whole application can cause performance issues. You can address this by configuring change detection to only run on a subset of the component tree.
6
6
7
-
If you are confident that a part of the application is not affected by a state change, you can use [OnPush](/api/core/ChangeDetectionStrategy) to skip change detection in an entire component subtree.
8
-
9
7
## Using `OnPush`
10
8
11
-
OnPush change detection instructs Angular to run change detection for a component subtree **only** when:
9
+
OnPush is the default change detection strategy in Angular (since v22). It instructs Angular to run change detection for a component subtree **only** when:
12
10
13
11
- The root component of the subtree receives new inputs as the result of a template binding. Angular compares the current and past value of the input with `==`.
14
12
- Angular handles an event _(for example using event binding, output binding, or `@HostListener` )_ in the subtree's root component or any of its children whether they are using OnPush change detection or not.
15
13
16
-
You can set the change detection strategy of a component to `OnPush` in the `@Component` decorator:
This section examines several common change detection scenarios to illustrate Angular's behavior.
30
17
31
-
### An event is handled by a component with default change detection
18
+
### An event is handled by a component with `Eager` change detection
32
19
33
-
If Angular handles an event within a component without `OnPush` strategy, the framework executes change detection on the entire component tree. Angular will skip descendant component subtrees with roots using `OnPush`, which have not received new inputs.
20
+
If Angular handles an event within a component with the `Eager` strategy, the framework executes change detection on the entire component tree. Angular will skip descendant component subtrees with roots using `OnPush`, which have not received new inputs.
34
21
35
22
As an example, if we set the change detection strategy of `MainComponent` to `OnPush` and the user interacts with a component outside the subtree with root `MainComponent`, Angular will check all the pink components from the diagram below (`AppComponent`, `HeaderComponent`, `SearchComponent`, `ButtonComponent`) unless `MainComponent` receives new inputs:
Copy file name to clipboardExpand all lines: adev/src/content/tutorials/signals/steps/1-creating-your-first-signal/README.md
-6Lines changed: 0 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,9 +116,3 @@ The toggle button is already in the template. Connect it to your `toggleStatus()
116
116
Congratulations! You've created your first signal and learned how to update it using both `set()` and `update()` methods. The `signal()` function creates a reactive value that Angular tracks, and when you update it, your UI automatically reflects the changes.
117
117
118
118
Next, you'll learn [how to derive state from signals using computed](/tutorials/signals/2-deriving-state-with-computed-signals)!
You might notice `ChangeDetectionStrategy.OnPush` in the component decorator throughout this tutorial. This is a performance optimization for Angular components that use signals. For now, you can safely ignore it—just know it helps your app run faster when using signals! You can learn more in the [change detection strategies API docs](/api/core/ChangeDetectionStrategy).
Copy file name to clipboardExpand all lines: adev/src/context/angular-20.mdc
-1Lines changed: 0 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,6 @@ This project adheres to modern Angular best practices, emphasizing maintainabili
64
64
userSelected = output<string>();
65
65
```
66
66
* **`computed()` for Derived State:** Use the `computed()` function from `@angular/core` for derived state based on signals.
67
-
* **`ChangeDetectionStrategy.OnPush`:** Always set `changeDetection: ChangeDetectionStrategy.OnPush` in the `@Component` decorator for performance benefits by reducing unnecessary change detection cycles.
68
67
* **Inline Templates:** Prefer inline templates (template: `...`) for small components to keep related code together. For larger templates, use external HTML files.
69
68
* **Reactive Forms:** Prefer Reactive forms over Template-driven forms for complex forms, validation, and dynamic controls due to their explicit, immutable, and synchronous nature.
70
69
* **No `ngClass` / `NgClass`:** Do not use the `ngClass` directive. Instead, use native `class` bindings for conditional styling.
0 commit comments