Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Change Detection Strategies | Section
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
Advanced Angular Patterns

bookChange Detection Strategies

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 7
some-alt