Using ngDoCheck for Custom Change Detection in Angular
📖 Scenario: You are building a simple Angular component that tracks a list of tasks. You want to detect changes to the tasks array manually using Angular's ngDoCheck lifecycle hook instead of relying on default change detection.This helps when you want to perform custom checks, like detecting changes inside an array or object that Angular's default change detection might miss.
🎯 Goal: Create an Angular standalone component that holds a list of tasks. Use ngDoCheck to detect when the tasks array changes by comparing its length. When a change is detected, update a message showing the number of tasks.
📋 What You'll Learn
Create a standalone Angular component named TaskListComponent with a tasks array containing exactly these strings: 'Buy groceries', 'Walk the dog', 'Read a book'
Add a variable previousTaskCount to store the previous length of the tasks array
Implement the ngDoCheck lifecycle hook to compare the current length of tasks with previousTaskCount
Update a message string to say 'Task list updated: X tasks' when a change is detected, where X is the current number of tasks
Add a button in the template to add a new task 'New Task' to the tasks array
Display the message and the list of tasks in the template
💡 Why This Matters
🌍 Real World
Custom change detection is useful when working with complex data structures or performance-sensitive apps where Angular's default detection is not enough or too costly.
💼 Career
Understanding ngDoCheck helps developers optimize Angular apps and handle edge cases in change detection, a valuable skill for frontend Angular developers.
Progress0 / 4 steps
1
Create the initial tasks array
Create a standalone Angular component named TaskListComponent. Inside it, create a public array called tasks with these exact strings: 'Buy groceries', 'Walk the dog', and 'Read a book'.
Angular
Hint
Remember to create the tasks array exactly with the three strings given.
2
Add previousTaskCount variable
Inside the TaskListComponent, add a public variable called previousTaskCount and set it to the length of the tasks array.
Angular
Hint
Set previousTaskCount equal to this.tasks.length.
3
Implement ngDoCheck to detect changes
Implement the ngDoCheck lifecycle hook method inside TaskListComponent. Inside it, compare this.tasks.length with this.previousTaskCount. If they differ, update this.previousTaskCount to the new length and set a public string variable message to 'Task list updated: X tasks', where X is the current number of tasks.
Angular
Hint
Use implements DoCheck and define the ngDoCheck() method. Compare lengths and update message accordingly.
4
Add template with button and display
Add the component template inside the @Component decorator. It should have a button with text Add Task that adds the string 'New Task' to the tasks array when clicked. Below the button, display the message in a paragraph and list all tasks using an unordered list with *ngFor.
Angular
Hint
Use (click)="addTask()" on the button and *ngFor="let task of tasks" to list tasks.
Practice
(1/5)
1. What is the main purpose of the ngDoCheck lifecycle hook in Angular?
easy
A. To run custom change detection logic when Angular's default detection misses changes.
B. To initialize component data before rendering.
C. To handle user input events like clicks and keypresses.
D. To destroy the component and clean up resources.
Hint: Use deep checks or immutable objects for internal changes [OK]
Common Mistakes:
Thinking !== detects internal object changes
Assuming ngDoCheck runs only once
Ignoring method existence or variable declaration
5. You want to detect changes inside a nested object profile in ngDoCheck. Which approach correctly detects changes to any property inside profile without replacing the whole object?
hard
A. Call detectChanges() manually inside ngDoCheck.
B. Only check if profile !== oldProfile using reference equality.
C. Use a deep comparison function inside ngDoCheck to compare profile with a saved copy.
D. Replace profile with a new object every time a property changes.
Solution
Step 1: Understand limitations of reference checks
Reference checks only detect if the object itself changed, not internal property changes.
Step 2: Use deep comparison for nested changes
A deep comparison function compares all nested properties to detect any internal changes.
Step 3: Avoid unnecessary object replacement
Replacing the whole object every time is inefficient; deep comparison allows detecting changes without replacement.
Final Answer:
Use a deep comparison function inside ngDoCheck to compare profile with a saved copy. -> Option C
Quick Check:
Deep compare nested objects for internal changes [OK]
Hint: Deep compare objects to detect internal changes [OK]
Common Mistakes:
Relying on reference equality for nested objects
Manually calling detectChanges inside ngDoCheck
Replacing whole object unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.