0
0
Angularframework~10 mins

ngDoCheck for custom change detection in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ngDoCheck for custom change detection
Component Initialized
Angular Change Detection Cycle
Call ngDoCheck()
Custom Checks Run
Detect Changes?
YesUpdate View
Cycle Ends
Angular runs its change detection cycle and calls ngDoCheck() where you can add your own checks to detect changes and update the view if needed.
Execution Sample
Angular
import { DoCheck } from '@angular/core';

export class MyComponent implements DoCheck {
  oldValue = '';
  value = '';
  ngDoCheck() {
    if (this.value !== this.oldValue) {
      this.oldValue = this.value;
      console.log('Value changed:', this.value);
    }
  }
}
This component uses ngDoCheck to detect when 'value' changes and logs the change.
Execution Table
StepvalueoldValueCondition (value !== oldValue)ActionOutput
1 (Init)''''FalseNo action
2 (Change value to 'A')'A'''TrueoldValue = 'A'; log changeValue changed: A
3 (Change detection cycle)'A''A'FalseNo action
4 (Change value to 'B')'B''A'TrueoldValue = 'B'; log changeValue changed: B
5 (Change detection cycle)'B''B'FalseNo action
6 (No change)'B''B'FalseNo action
💡 No further changes detected; ngDoCheck ends without updates.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
value'''A''B''B'
oldValue'''A''B''B'
Key Moments - 2 Insights
Why does ngDoCheck run even when the value hasn't changed?
ngDoCheck is called every Angular change detection cycle (see steps 3,5,6) to allow custom checks, but your condition inside ngDoCheck controls if any action happens.
What happens if you forget to update oldValue inside ngDoCheck?
If oldValue is not updated (see step 2 vs step 4), the condition will always be true and ngDoCheck will log changes repeatedly, causing unnecessary updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is oldValue after step 4?
A'B'
B''
C'A'
D'C'
💡 Hint
Check the 'oldValue' column in the execution_table at step 4.
At which step does the condition (value !== oldValue) become false after a change?
AStep 4
BStep 2
CStep 3
DStep 6
💡 Hint
Look at the 'Condition' column after step 2 and step 3 in the execution_table.
If you set value to 'C' at step 5, what will happen in the next ngDoCheck call?
ANo action, condition false
BoldValue updates to 'C' and logs change
CError because oldValue is not updated
DngDoCheck stops running
💡 Hint
Refer to how ngDoCheck updates oldValue and logs changes when condition is true in the execution_table.
Concept Snapshot
ngDoCheck is a lifecycle hook Angular calls every change detection cycle.
Use it to run custom change detection logic.
Inside ngDoCheck, compare current and previous values.
If changed, update old values and trigger needed actions.
Helps detect changes Angular's default detection misses.
Full Transcript
This example shows how Angular calls ngDoCheck every change detection cycle. The component tracks a value and an oldValue. When value changes, ngDoCheck detects it by comparing value and oldValue. If different, it updates oldValue and logs the change. If not, it does nothing. This way, you can add your own checks beyond Angular's default detection. The execution table shows each step: initialization, value changes, and detection cycles. The variable tracker shows how value and oldValue update over time. Key moments clarify why ngDoCheck runs often and why updating oldValue is important. The quiz tests understanding of variable states and condition results during the cycle.