Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Change Detection Strategies | Section
Advanced Angular Patterns

bookChange Detection Strategies

Swipe to show menu

Understanding how Angular detects and responds to changes in your application is essential for building high-performance interfaces. Angular's change detection mechanism automatically checks for updates in your component tree whenever an event occurs, such as user input, HTTP responses, or timer events. By default, Angular uses a strategy that checks every component in the tree on each change detection cycle, ensuring UI consistency but potentially impacting performance, especially in large or complex applications. Optimizing change detection can significantly reduce unnecessary computations and improve responsiveness.

parent.component.ts

parent.component.ts

child-default.component.ts

child-default.component.ts

child-onpush.component.ts

child-onpush.component.ts

copy

When using the OnPush strategy, Angular optimizes change detection by only checking the component when its input references change, an event originates from the component, or you explicitly request a check. However, sometimes you may need to trigger a manual update, such as after updating an object property without changing its reference. In these cases, you can use Angular's ChangeDetectorRef service. By injecting ChangeDetectorRef into your component, you can call methods like markForCheck() or detectChanges() to control when Angular checks and updates the component's view. Referring back to the OnPush example, if you mutate the data object in place, you would use ChangeDetectorRef.markForCheck() to ensure the child component reflects the change.

question mark

Which approach is most effective for improving performance in an Angular component that receives data via inputs and rarely changes?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 7
some-alt