Thuta Learning
Angular
AdvancedWeb DevelopmentIntermediate

Change Detection

AngularLesson 4025 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Change Detection
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

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.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of Change Detection

Core syntax or pattern

typescript
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

  1. input and stateimport { ChangeDetectionStrategy, ChangeDetectorRef }...

    Change Detection is the mechanism Angular uses to detect changes in a component's data and update the view accordingly.

  2. component and templatetemplate: `<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.

  3. rendered Angular UI}

    Create an optimized child with an object input and compare nested mutation with immutable replacement, then add computed derived state.

Execution flow
  1. input and state
  2. component and template
  3. 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.

typescript
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
  }
}
You should see
(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
typescript
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

What best describes the core idea of Change Detection?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Calling detectChanges everywhere can hide the actual state-ownership or timing bug.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

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.

You'll know it worked when: (Using the OnPush strategy improves performance by cutting down unnecessary checks)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Change Detection | Thuta Learning