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

bookChange Detection Strategies

Deslize para mostrar o 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 7

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 7
some-alt