0
0
Angularframework~10 mins

Default change detection strategy in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default change detection strategy
Event triggers (click, async, etc.)
Angular runs change detection
Traverse component tree
Check each component's bindings
Update DOM if data changed
Finish change detection cycle
Angular's default change detection runs on every event and checks all components for data changes, updating the DOM accordingly.
Execution Sample
Angular
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<button (click)="increment()">Increment</button><p>{{count}}</p>`
})
export class AppComponent {
  count = 0;
  increment() { this.count++; }
}
A simple Angular component with a button that increments a count displayed on screen, triggering default change detection.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Initial loadcount=0Render componentcount=0AppComponentDisplay count=0
2User clicks buttoncount=0Call increment()count=1AppComponentUpdate displayed count to 1
3Change detection runscount=1Check bindingscount=1AppComponentNo further changes
4User clicks button againcount=1Call increment()count=2AppComponentUpdate displayed count to 2
5Change detection runscount=2Check bindingscount=2AppComponentNo further changes
6No more eventscount=2No actioncount=2NoneNo DOM changes
💡 No more events trigger change detection, so Angular stops checking.
Variable Tracker
VariableStartAfter 1After 2Final
count0122
Key Moments - 2 Insights
Why does Angular check all components on every event?
Because the default strategy runs change detection for the entire component tree on every event, ensuring all data bindings are up to date as shown in steps 2 and 4 of the execution_table.
Does Angular update the DOM if the data hasn't changed?
No, Angular compares previous and current values during change detection and only updates the DOM if data changed, as seen in steps 3 and 5 where no DOM changes occur.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after step 2?
A2
B0
C1
DUndefined
💡 Hint
Check the 'State After' column in row for step 2.
At which step does Angular update the DOM to show 'count' as 2?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the step where 'count' changes to 2 and DOM updates.
If no user clicks happen, what does Angular do according to the execution table?
ARuns change detection continuously
BDoes nothing after initial load
CUpdates DOM repeatedly
DThrows an error
💡 Hint
See step 6 where no events trigger change detection.
Concept Snapshot
Default change detection in Angular:
- Runs on every event (click, async, etc.)
- Checks all components in the tree
- Compares data bindings
- Updates DOM only if data changed
- Ensures UI stays in sync automatically
Full Transcript
Angular's default change detection strategy runs after every event like clicks or async operations. It traverses the entire component tree checking each component's data bindings. If it finds any changes, it updates the DOM to reflect the new data. For example, when a button click increments a count variable, Angular detects the change and updates the displayed count. If no changes occur, Angular skips DOM updates to optimize performance. This process repeats on every event, keeping the UI in sync with the data automatically.