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
Basic Syntax
A component can implement the OnChanges interface and define the ngOnChanges() method.
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.
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
);
}
}
}
<app-course
[courseName]="selectedCourse">
</app-course>
selectedCourse = 'Java Full Stack';
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.
@Input() courseName = '';
ngOnChanges(changes: SimpleChanges): void {
const change = changes['courseName'];
if (change) {
console.log(
'Previous:',
change.previousValue
);
console.log(
'Current:',
change.currentValue
);
}
}
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.
@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');
}
}
}
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.
@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
);
}
}
<app-course
[courseName]="course"
[trainingMode]="mode">
</app-course>
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.
@Input() searchText = '';
message = '';
ngOnChanges(changes: SimpleChanges): void {
if (changes['searchText']) {
const value =
changes['searchText'].currentValue;
this.message =
'Searching for: ' + value;
}
}
<p>
{{ message }}
</p>
@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.
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;
}
}
}
<app-course-details
[courseName]="selectedCourse"
[trainingMode]="selectedMode">
</app-course-details>
selectedCourse = 'Java Full Stack';
selectedMode = 'Offline';
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.