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

bookChange Detection Strategies

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 7

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 7
some-alt