Explain it simply
Change Detection, in plain language
Change Detection is the mechanism Angular uses to detect changes in a component's data and update the view accordingly.
Problem → Why → Solution
The problem — why this concept matters
Calling detectChanges everywhere can hide the actual state-ownership or timing bug.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
Change detection reevaluates template bindings after state changes and decides which DOM updates are needed. Optimized strategies, immutable references, signals or Observables, and stable list tracking improve predictability and reduce work. Measure with profiling before optimizing, looking for expensive template work, broad mutable state, and unnecessary emissions.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Change Detection
Core syntax or pattern
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
// Default Change Detection (checks all the time)
@Component({
selector: 'app-default',
template: `<p>{{ data }}</p>`
})
export class DefaultComponent {
data = 'Hello';
}
// OnPush Change Detection (checks only when @Input changes)
@Component({
selector: 'app-optimized',
template: `<p>{{ data }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OptimizedComponent {
@Input() data: any;
constructor(private cdr: ChangeDetectorRef) {}
// Manually trigger change detection if needed
updateData() {
this.data = 'New value';
this.cdr.markForCheck(); // Tell Angular to check
}
}Change detection reevaluates template bindings after state changes and decides which DOM updates are needed. Optimized strategies, immutable references, signals or Observables, and stable list tracking improve predictability and reduce work. Measure with profiling before optimizing, looking for expensive template work, broad mutable state, and unnecessary emissions.
Read the code step by step
- input and state
import { ChangeDetectionStrategy, ChangeDetectorRef }...Change Detection is the mechanism Angular uses to detect changes in a component's data and update the view accordingly.
- component and template
template: `<p>{{ data }}</p>`,Change detection reevaluates template bindings after state changes and decides which DOM updates are needed. Optimized strategies, immutable references, signals or Observables, and stable list tracking improve predictability and reduce work. Measure with profiling before optimizing, looking for expensive template work, broad mutable state, and unnecessary emissions.
- rendered Angular UI
}Create an optimized child with an object input and compare nested mutation with immutable replacement, then add computed derived state.
- input and state
- component and template
- rendered Angular UI
Success
Explain the core concept and Angular data flow of Change Detection
Failure
Calling detectChanges everywhere can hide the actual state-ownership or timing bug.
Change Detection is the mechanism Angular uses to detect changes in a component's data and update the view accordingly.
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
// Default Change Detection (checks all the time)
@Component({
selector: 'app-default',
template: `<p>{{ data }}</p>`
})
export class DefaultComponent {
data = 'Hello';
}
// OnPush Change Detection (checks only when @Input changes)
@Component({
selector: 'app-optimized',
template: `<p>{{ data }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OptimizedComponent {
@Input() data: any;
constructor(private cdr: ChangeDetectorRef) {}
// Manually trigger change detection if needed
updateData() {
this.data = 'New value';
this.cdr.markForCheck(); // Tell Angular to check
}
}(Using the OnPush strategy improves performance by cutting down unnecessary checks)Real-world usage
How to apply it in a real project
Create an optimized child with an object input and compare nested mutation with immutable replacement, then add computed derived state.
Mini challenge
Build it yourself
Rebuild the Change Detection example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
// Default Change Detection (checks all the time)
@Component({
selector: 'app-default',
template: `<p>{{ data }}</p>`
})
export class DefaultComponent {
data = 'Hello';
}
// OnPush Change Detection (checks only when @Input changes)
@Component({
selector: 'app-optimized',
template: `<p>{{ data }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OptimizedComponent {
@Input() data: any;
constructor(private cdr: ChangeDetectorRef) {}
// Manually trigger change detection if needed
updateData() {
this.data = 'New value';
this.cdr.markForCheck(); // Tell Angular to check
}
}Expected: Explain the core concept and Angular data flow of Change Detection
Show hint
Create an optimized child with an object input and compare nested mutation with immutable replacement, then add computed derived state.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → component and template → rendered Angular UI
- Change detection reevaluates template bindings after state changes and decides which DOM updates are needed. Optimized strategies, immutable references, signals or Observables, and stable list tracking improve predictability and reduce work. Measure with profiling before optimizing, looking for expensive template work, broad mutable state, and unnecessary emissions.
- Calling detectChanges everywhere can hide the actual state-ownership or timing bug.
- Create an optimized child with an object input and compare nested mutation with immutable replacement, then add computed derived state.
Angular — Signals — Angular