0
0
Angularframework~5 mins

ngDoCheck for custom change detection in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of ngDoCheck in Angular?

ngDoCheck is a lifecycle hook that lets you run custom change detection logic in a component. It runs during every change detection cycle, allowing you to check for changes Angular might miss.

Click to reveal answer
beginner
When is ngDoCheck called in the Angular component lifecycle?

ngDoCheck is called after ngOnChanges and before ngAfterContentChecked during every change detection run.

Click to reveal answer
intermediate
Why would you use ngDoCheck instead of relying on Angular's default change detection?

You use ngDoCheck when Angular's default change detection does not detect changes, such as when mutating objects or arrays directly instead of replacing them.

Click to reveal answer
intermediate
What is a common pattern to detect changes inside ngDoCheck?

A common pattern is to store a previous value and compare it with the current value inside ngDoCheck. If they differ, you know a change happened.

Click to reveal answer
beginner
What should you be careful about when using ngDoCheck?

Because ngDoCheck runs very often, avoid heavy or slow operations inside it to keep your app fast and responsive.

Click to reveal answer
What does ngDoCheck allow you to do in Angular?
AInitialize component inputs
BDestroy component resources
CRun custom change detection logic
DHandle user events
When is ngDoCheck called?
AAfter every change detection cycle
BOnly when inputs change
CBefore <code>ngOnInit</code>
DOnly once when the component is created
Which scenario is a good reason to use ngDoCheck?
AWhen you want to detect changes in a mutated array
BWhen you want to listen to click events
CWhen you want to fetch data from a server
DWhen you want to style a component
What is a recommended practice inside ngDoCheck?
ARun heavy calculations
BMake HTTP requests
CModify DOM elements directly
DCompare previous and current values to detect changes
What should you avoid inside ngDoCheck to keep your app fast?
ASimple value comparisons
BHeavy or slow operations
CLogging to console
DUpdating component inputs
Explain how ngDoCheck helps with custom change detection in Angular.
Think about when Angular's default detection might not notice changes.
You got /4 concepts.
    Describe best practices to follow when implementing ngDoCheck.
    Consider performance impact since it runs often.
    You got /4 concepts.