Angular Tutorial 0/42 lessons ~6 min read Lesson 24

    Angular Change Detection

    Learn Angular Change Detection, including Zone.js, Default and OnPush strategies, the change detection tree, immutable updates, Signals integration, ChangeDetectorRef (markForCheck, detectChanges, detach, reattach), zoneless Angular, performance optimization, enterprise architecture, common mistakes, and interview questions.

    Course progress0%
    Focus
    32 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Learning Objectives

    By the end of this lesson, you will understand:

    • What Change Detection is.
    • Why Angular needs Change Detection.
    • The complete Angular rendering lifecycle.
    • How Zone.js works.
    • Default Change Detection Strategy.
    • OnPush Change Detection Strategy.
    • Change Detection Tree.
    • Component Dirty Checking.
    • Immutable vs Mutable Objects.
    • How Signals integrate with Change Detection.
    • How ChangeDetectorRef works.
    • markForCheck().
    • detectChanges().
    • detach() and reattach().
    • Zoneless Angular.
    • Change Detection optimization techniques.
    • Performance bottlenecks.
    • Enterprise best practices.
    • Real-world architecture.
    • Common mistakes.
    • Advanced interview questions.

    Introduction

    Imagine you're building TechLearningPro.

    A student clicks:

    text
    Complete Lesson

    Immediately:

    • Progress should become 72% → 76%
    • Sidebar should update
    • Progress bar should update
    • Certificate eligibility should update
    • Lesson button should become Completed

    The developer never manually redraws the page.

    Angular automatically updates the UI.

    This process is called Change Detection.

    What is Change Detection?

    Change Detection is Angular's mechanism for keeping the UI synchronized with application state.

    Whenever application data changes, Angular determines:

    • What changed?
    • Which component depends on it?
    • Which DOM elements need updating?

    Architecture:

    text
    Application State
    Change Detection
    Compare Values
    Update DOM

    Why Do We Need Change Detection?

    Suppose:

    typescript
    username = 'Jagannath';

    Template:

    html
    <h2>{{ username }}</h2>

    Later:

    typescript
    username = 'Krishna';

    Without Change Detection:

    text
    Browser
    Still Shows
    Jagannath

    With Angular:

    text
    username Changes
    Angular Detects Change
    DOM Updated
    Krishna

    Angular Rendering Lifecycle

    When data changes:

    text
    User Action
    Component State Changes
    Change Detection Starts
    Angular Checks Bindings
    DOM Updates
    Browser Paint

    What Triggers Change Detection?

    Angular normally starts Change Detection after events such as:

    • Button clicks
    • Keyboard input
    • HTTP responses
    • Timers (setTimeout, setInterval)
    • Promise resolution
    • Observable emissions
    • Signal updates

    Example:

    text
    Button Click
    Counter++
    Change Detection
    UI Updates

    Zone.js

    Historically Angular relied on Zone.js.

    Zone.js monitors asynchronous operations.

    Examples:

    text
    Click Event
    HTTP Request
    Promise
    Timer
    Observable

    Architecture:

    text
    Browser Event
    Zone.js
    Angular
    Change Detection

    Zone.js automatically informs Angular that something may have changed.

    Example with Button Click

    typescript
    count++;

    Flow:

    text
    Button Click
    Zone.js
    Angular
    Change Detection
    DOM Updated

    The developer doesn't manually refresh the screen.

    Default Change Detection Strategy

    Every component uses Default strategy unless specified otherwise.

    Example:

    typescript
    @Component({
    changeDetection:
    ChangeDetectionStrategy.Default
    })

    Architecture:

    text
    Application
    Root Component
    Child
    Child
    Child
    Entire Tree Checked

    Default strategy checks every component in the affected tree.

    Example

    Suppose:

    text
    App
    ├── Header
    ├── Dashboard
    ├── Footer

    A button inside Dashboard changes.

    With Default strategy:

    text
    App
    Header Checked
    Dashboard Checked
    Footer Checked

    Even if Header and Footer didn't change.

    Change Detection Tree

    Angular organizes components into a tree.

    text
    AppComponent
    ├── Header
    ├── Sidebar
    ├── Dashboard
    │ │
    │ ├── Statistics
    │ ├── Courses
    │ └── Notifications
    └── Footer

    Angular traverses this tree during Change Detection.

    OnPush Change Detection

    Large applications benefit from:

    text
    OnPush

    Example:

    typescript
    @Component({
    changeDetection:
    ChangeDetectionStrategy.OnPush
    })

    Architecture:

    text
    App
    Dashboard (OnPush)
    Only Updates
    When Needed

    Instead of checking continuously.

    When Does OnPush Run?

    An OnPush component updates when:

    • An @Input() reference changes.
    • An event occurs inside the component.
    • A Signal changes.
    • markForCheck() is called.
    • detectChanges() is called.

    Architecture:

    text
    OnPush Component
    Input Changed?
    Signal Changed?
    markForCheck()?
    Update

    Why OnPush is Faster

    Default:

    text
    100 Components
    Check All

    OnPush:

    text
    100 Components
    Only 4 Need Update
    Check 4

    This greatly reduces unnecessary work.

    Mutable Objects Problem

    Suppose:

    typescript
    user = {
    name: 'Jagannath'
    };

    Later:

    typescript
    user.name = 'Krishna';

    The object reference didn't change.

    For OnPush this can be problematic.

    Immutable Update

    Preferred:

    typescript
    user = {
    ...user,
    name: 'Krishna'
    };

    Architecture:

    text
    Old Object
    New Object
    Reference Changes
    OnPush Updates

    Signals and Change Detection

    Signals simplify Change Detection.

    Example:

    typescript
    username = signal('Jagannath');

    Later:

    typescript
    username.set('Krishna');

    Architecture:

    text
    Signal
    Changed
    Dependent Component
    Update

    Angular knows exactly what depends on the Signal.

    ChangeDetectorRef

    Sometimes developers manually control Change Detection.

    Inject:

    typescript
    constructor(
    private cd:
    ChangeDetectorRef
    ){}

    Useful APIs:

    text
    markForCheck()
    detectChanges()
    detach()
    reattach()

    markForCheck()

    Marks an OnPush component for checking during the next Change Detection cycle.

    Example:

    typescript
    this.cd.markForCheck();

    Architecture:

    text
    Background Update
    markForCheck()
    Next Change Detection
    UI Updated

    detectChanges()

    Runs Change Detection immediately for the current component.

    Example:

    typescript
    this.cd.detectChanges();

    Architecture:

    text
    Component
    detectChanges()
    Immediate Refresh

    Use sparingly.

    detach()

    Stops automatic Change Detection.

    Example:

    typescript
    this.cd.detach();

    Architecture:

    text
    Component
    Detached
    Angular Ignores

    Useful for high-frequency dashboards.

    reattach()

    Enable Change Detection again.

    typescript
    this.cd.reattach();

    Example

    Live Stock Dashboard

    Thousands of updates arrive every second.

    Instead of:

    text
    Update UI
    1000 Times

    Detach:

    text
    Updates
    Store Values
    Every Second
    detectChanges()
    Refresh UI Once

    Huge performance improvement.

    Signals + OnPush

    Modern Angular applications combine:

    text
    Signals
    +
    OnPush

    Architecture:

    text
    Signal
    Component
    (OnPush)
    Update Only
    When Signal Changes

    This is Angular's recommended modern approach.

    Zoneless Angular

    Angular is evolving toward Zoneless Change Detection.

    Instead of relying on Zone.js:

    text
    Application
    Signals
    Explicit Reactivity
    UI Updates

    Benefits:

    • Better performance
    • Simpler debugging
    • Predictable rendering
    • Less framework overhead

    Enterprise Architecture

    TechLearningPro

    text
    Backend API
    Course Store
    Signals
    Computed
    OnPush Components
    UI

    Each feature owns its own state.

    Performance Optimization

    Prefer:

    text
    Signals
    OnPush
    Lazy Loading
    Virtual Scrolling
    Computed Signals
    Pure Pipes

    Avoid:

    text
    Huge Components
    Mutable Objects
    Heavy Templates
    Repeated Function Calls
    Massive Lists

    Common Mistakes

    Mutating Objects

    Avoid:

    typescript
    course.title = 'Angular';

    Prefer immutable updates.

    Calling Functions in Templates

    Avoid:

    html
    {{ calculateTotal() }}

    Instead:

    typescript
    total = computed(...);

    Default Strategy Everywhere

    Large enterprise applications benefit from OnPush.

    Overusing detectChanges()

    It should not become your primary solution.

    Fix the architecture first.

    Massive Components

    Break UI into:

    text
    Header
    Sidebar
    Content
    Footer
    Widgets

    Smaller components reduce rendering work.

    Real-Time Example

    TechLearningPro Course Page

    text
    Course Store
    Signals
    Progress Signal
    Sidebar
    Progress Bar
    Certificate Widget
    Only These Update

    The rest of the page remains untouched.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    10 questions
    1BeginnerQuestionWhat is Angular Change Detection?+

    Answer

    Change Detection is Angular's mechanism for synchronizing the UI with application state by updating only the affected bindings after state changes.
    2BeginnerQuestionWhat triggers Change Detection?+

    Answer

    Common triggers include:
    • User events
    • HTTP responses
    • Timers
    • Promises
    • Observable emissions
    • Signal updates
    3BeginnerQuestionWhat is Zone.js?+

    Answer

    Zone.js patches asynchronous browser APIs and notifies Angular when asynchronous work completes so Angular can run Change Detection automatically.
    4IntermediateQuestionDifference between Default and OnPush?+

    Answer

    Default checks more broadly through the component tree, is easier to use, and is more automatic. OnPush checks primarily when specific triggers occur, offers better performance for many applications, and encourages predictable immutable patterns.
    5IntermediateQuestionWhy is OnPush faster?+

    Answer

    Because Angular avoids checking components unnecessarily and focuses on components that have valid update triggers.
    6IntermediateQuestionWhat is ChangeDetectorRef?+

    Answer

    It provides APIs to manually influence Change Detection, including markForCheck(), detectChanges(), detach(), and reattach().
    7IntermediateQuestionDifference between `markForCheck()` and `detectChanges()`?+

    Answer

    • markForCheck() schedules the component to be checked in the next Change Detection cycle.
    • detectChanges() performs an immediate Change Detection pass for the current component and its children.
    8IntermediateQuestionWhy are immutable objects important?+

    Answer

    Immutable updates create new object references, making it easier for Angular to recognize meaningful changes, especially with OnPush.
    9AdvancedQuestionHow do Signals improve Change Detection?+

    Answer

    Signals provide fine-grained reactivity. Angular knows exactly which parts of the UI depend on a Signal and updates only those parts when the Signal changes.
    10AdvancedQuestionWhat is the recommended Angular architecture today?+

    Answer

    Backend API → Services → Signal Store → Computed Signals → OnPush Components → UI. This architecture offers excellent scalability, maintainability, and performance.

    Summary

    Angular Change Detection is the engine that keeps your application responsive and efficient.

    text
    User Action / API / Signal
    State Changes
    Change Detection Engine
    ┌───────┼────────┐
    ▼ ▼ ▼
    Signals OnPush Default
    Update Required UI
    Browser DOM

    For a modern enterprise application like TechLearningPro, the ideal flow is:

    text
    Backend API
    Feature Service
    Signal Store
    Computed Signals
    OnPush Components
    Minimal DOM Updates
    Fast & Responsive UI

    The core principle: Keep state reactive with Signals, use OnPush for efficient rendering, update data immutably, and let Angular refresh only what actually changed.

    Next Lesson

    Angular Dynamic Components — In the next lesson, you will learn:

    • What Dynamic Components are.
    • Why they are useful.
    • ViewContainerRef.
    • createComponent().
    • Dynamic component injection.
    • Passing inputs and outputs.
    • Dynamic dialogs and modals.
    • Plugin architecture.
    • Dashboard widget systems.
    • Lazy loading dynamic components.
    • Enterprise use cases.
    • Performance optimization.
    • Advanced interview questions.
    Ready to mark this lesson complete?Track your journey across the entire course.