Master Angular From Scratch

Learn Angular with clear, interactive, and structured lessons for beginners and developers.

ANGULAR COURSE SYLLABUS
1. Angular Introduction
Angular Introduction Angular History Angular Fundamentals Angular Architecture Component-Based Angular
2. Angular Setup
Angular Setup Introduction Installing Node.js Installing Angular CLI Creating an Angular Application Angular Project Structure
3. Components
Components Overview Component Anatomy Creating Components Component Selector Component Template Component Styles Component Metadata Standalone Components Parent and Child Components Reusable Components
4. Templates
Templates Introduction Template Syntax Template Expressions Template Statements Template Reference Variables ng-template ng-container ngTemplateOutlet Built-in Control Flow
5. Data Binding
Data Binding Introduction Interpolation Binding Property Binding Event Binding Two-Way Data Binding Class & Style Binding
6. Directives
Directives Introduction Attribute Directives Structural Directives Custom Directives
7. Pipes
Pipes Introduction Built-in Pipes Parameterized Pipes Chaining Pipes Custom Pipes
8. Event Handling
Event Handling Introduction DOM Events Event Binding Syntax Event Object Passing Data to Event Handlers
9. Input & Output
Component Communication @Input @Output EventEmitter
10. Lifecycle Hooks
Lifecycle Hooks Introduction ngOnInit ngOnChanges ngAfterViewInit ngOnDestroy
11. Services
Services Introduction Creating Angular Services Using Services in Components Sharing Data Through Services Service Scope
12. Dependency Injection
Dependency Injection Introduction Injecting Dependencies InjectionToken Provider Configuration
13. Routing
Routing Introduction Route Configuration RouterLink & Navigation Route Parameters Child Routes Route Guards
14. Forms
Angular Forms Introduction Template-Driven Forms Reactive Forms Form Validation Form Submission & Error Handling
15. HttpClient
HttpClient Introduction Configuring HttpClient GET Request POST, PUT & DELETE Requests Headers & Parameters HTTP Error Handling
16. Observables
Observables Introduction Creating & Subscribing to Observables Observable Data Streams Observable Operators Subscription Management
17. RxJS
RxJS Introduction RxJS Operators Transformation & Filtering Combining Observables Higher-Order Mapping Subjects
18. API Integration
API Integration Introduction Connecting Angular with REST API Consuming API Data CRUD API Integration API Response & Error Handling

Angular OnChanges

Angular @OnChanges is a lifecycle hook used to detect changes in input-bound properties of a component. It is useful when a child component needs to react whenever data received from its parent changes.

What is @OnChanges?

The ngOnChanges() lifecycle hook is called when one or more data-bound input properties of a component change.

It receives a SimpleChanges object containing information about the previous value and the current value of the changed input property.

Simple Definition

ngOnChanges() allows a component to respond when data received through @Input changes.

How @OnChanges Works

Parent Component Sends data
→
@Input Receives value
→
ngOnChanges() Detects change
→
Component UI Updates accordingly

Basic Syntax

A component can implement the OnChanges interface and define the ngOnChanges() method.

Component
import { Component, Input, OnChanges, SimpleChanges }
from '@angular/core';

@Component({
    selector: 'app-course',
    templateUrl: './course.component.html'
})
export class CourseComponent implements OnChanges {

    @Input() courseName = '';

    ngOnChanges(changes: SimpleChanges): void {

        console.log(changes);

    }

}
Important

Angular automatically calls ngOnChanges() whenever an input-bound property receives a changed value.

What is SimpleChanges?

The SimpleChanges object contains information about the input properties that changed.

Previous Value

Contains the value of the input before the change.

previousValue
Current Value

Contains the latest value received by the component.

currentValue
First Change

Indicates whether this is the first detected change.

firstChange
Changes Object

Gives access to all changed input properties.

SimpleChanges

1. Simple OnChanges Example

In this example, the child component receives a course name through @Input and reacts when that value changes.

Child Component
import { Component, Input, OnChanges, SimpleChanges }
from '@angular/core';

@Component({
    selector: 'app-course',
    templateUrl: './course.component.html'
})
export class CourseComponent implements OnChanges {

    @Input() courseName = '';

    ngOnChanges(changes: SimpleChanges): void {

        if (changes['courseName']) {

            console.log(
                'Course changed to:',
                changes['courseName'].currentValue
            );

        }

    }

}
Parent Template
<app-course
    [courseName]="selectedCourse">
</app-course>
Parent Component
selectedCourse = 'Java Full Stack';
Expected Behavior:

Initial Course: Java Full Stack

When the parent changes the course value, ngOnChanges() detects the change.

2. Previous Value and Current Value

Angular provides both the previous and current values through the SimpleChange object.

Component
@Input() courseName = '';

ngOnChanges(changes: SimpleChanges): void {

    const change = changes['courseName'];

    if (change) {

        console.log(
            'Previous:',
            change.previousValue
        );

        console.log(
            'Current:',
            change.currentValue
        );

    }

}
Previous Value Java Full Stack
→
Input Change Parent updates value
→
Current Value Python Full Stack

3. Checking First Change

The firstChange property can be used to determine whether the current change is the first change detected for an input property.

Example
@Input() courseName = '';

ngOnChanges(changes: SimpleChanges): void {

    const change = changes['courseName'];

    if (change) {

        if (change.firstChange) {

            console.log('First course value received');

        } else {

            console.log('Course value changed');

        }

    }

}
Expected Behavior:

First value: firstChange = true

Later updates: firstChange = false

4. Detecting Multiple Input Changes

A component can have multiple input properties. The SimpleChanges object can contain information about each property that changed.

Component
@Input() courseName = '';

@Input() trainingMode = '';

ngOnChanges(changes: SimpleChanges): void {

    if (changes['courseName']) {

        console.log(
            'Course:',
            changes['courseName'].currentValue
        );

    }

    if (changes['trainingMode']) {

        console.log(
            'Mode:',
            changes['trainingMode'].currentValue
        );

    }

}
Parent Template
<app-course
    [courseName]="course"
    [trainingMode]="mode">
</app-course>
Example Values:

Course: .NET Full Stack

Training Mode: Offline

5. Reacting to Input Changes

One of the important uses of ngOnChanges() is to perform some action whenever an input value changes.

Example
@Input() searchText = '';

message = '';

ngOnChanges(changes: SimpleChanges): void {

    if (changes['searchText']) {

        const value =
            changes['searchText'].currentValue;

        this.message =
            'Searching for: ' + value;

    }

}
Template
<p>
    {{ message }}
</p>
Expected Output:
Searching for: Angular

@OnChanges vs @OnInit

ngOnChanges() ngOnInit()
Responds to changes in input-bound properties. Performs component initialization.
Can run whenever an input value changes. Normally runs once during initialization.
Receives a SimpleChanges object. Does not receive SimpleChanges.
Useful for reacting to parent-to-child data changes. Useful for setting up initial component state.

Complete CIIT Training Institute Example

Suppose the CIIT Training Institute course dashboard sends a selected course and training mode to a child component. The child component can react whenever these values change.

course-details.component.ts
import {
    Component,
    Input,
    OnChanges,
    SimpleChanges
} from '@angular/core';

@Component({
    selector: 'app-course-details',
    templateUrl: './course-details.component.html'
})
export class CourseDetailsComponent
    implements OnChanges {

    @Input() courseName = '';

    @Input() trainingMode = '';

    courseMessage = '';

    ngOnChanges(changes: SimpleChanges): void {

        if (
            changes['courseName'] ||
            changes['trainingMode']
        ) {

            this.courseMessage =
                this.courseName +
                ' - ' +
                this.trainingMode;

        }

    }

}
Parent Template
<app-course-details
    [courseName]="selectedCourse"
    [trainingMode]="selectedMode">
</app-course-details>
Parent Component
selectedCourse = 'Java Full Stack';

selectedMode = 'Offline';
Expected Output:

Selected Course

Course: Java Full Stack

Training Mode: Offline

Message: Java Full Stack - Offline

Important Points About @OnChanges

  • ngOnChanges() is related to input-bound properties.
  • It receives a SimpleChanges object.
  • previousValue provides the earlier value.
  • currentValue provides the latest value.
  • firstChange indicates whether it is the first detected change.
  • It is especially useful for reacting to parent-to-child data updates.

@OnChanges Best Practices

  • Use ngOnChanges when component behavior depends on input changes.
  • Check the required property before processing its change.
  • Use currentValue and previousValue when comparison is needed.
  • Keep change-handling logic simple and readable.
  • Move reusable business logic into services when appropriate.
  • Avoid unnecessary processing when unrelated inputs change.

Summary

Angular @OnChanges provides the ngOnChanges() lifecycle method for responding to changes in input-bound properties.

The SimpleChanges object provides useful information such as previousValue, currentValue, and firstChange.