Angular Lifecycle Hooks
Learn how Angular lifecycle hooks allow you to execute code at specific stages of a component's lifecycle.
What are Lifecycle Hooks?
Angular components go through different stages from creation to destruction. These stages are called the component lifecycle.
Angular provides lifecycle hook methods that allow developers to execute custom code when a particular lifecycle stage occurs.
Lifecycle hooks are useful for initialization, responding to changes, working with the view, and performing cleanup operations.
Angular Component Lifecycle
Common Lifecycle Hooks
@OnChanges
Runs when an Input property receives a new value.
@OnInit
Runs once after Angular initializes the component.
@AfterViewInit
Runs after Angular initializes the component view.
@OnDestroy
Runs just before Angular destroys the component.
@OnInit
The @OnInit lifecycle hook runs once after Angular has initialized the component's data-bound properties.
It is commonly used for initialization tasks such as loading course data, calling services, or preparing component properties.
import {
Component,
OnInit
} from '@angular/core';
@Component({
selector: 'app-courses',
templateUrl: './courses.component.html'
})
export class CoursesComponent
implements OnInit {
courses: string[] = [];
ngOnInit() {
this.courses = [
'Java Full Stack',
'Python Full Stack',
'Full Stack .NET'
];
}
}
@OnChanges
The @OnChanges hook runs when one or more @Input properties change.
It is useful when a child component needs to respond to new data received from its parent.
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 = '';
ngOnChanges(changes: SimpleChanges) {
console.log(
'Course changed:',
changes['courseName']
);
}
}
@AfterViewInit
The @AfterViewInit hook runs after Angular has completely initialized the component's view.
It can be useful when code needs to work with elements that are available after the view has been initialized.
import {
Component,
AfterViewInit
} from '@angular/core';
@Component({
selector: 'app-course',
templateUrl: './course.component.html'
})
export class CourseComponent
implements AfterViewInit {
ngAfterViewInit() {
console.log(
'Course view initialized'
);
}
}
@OnDestroy
The @OnDestroy hook runs immediately before Angular destroys a component.
It is commonly used for cleanup tasks such as unsubscribing from Observables, removing event listeners, or stopping timers.
import {
Component,
OnDestroy
} from '@angular/core';
@Component({
selector: 'app-course',
templateUrl: './course.component.html'
})
export class CourseComponent
implements OnDestroy {
ngOnDestroy() {
console.log(
'Course component destroyed'
);
}
}
Lifecycle Hooks Comparison
| Hook | When It Runs | Common Use |
|---|---|---|
| @OnChanges | When Input values change | React to new Input data |
| @OnInit | After component initialization | Initialize data |
| @AfterViewInit | After view initialization | Work with initialized view |
| @OnDestroy | Before component destruction | Cleanup operations |
Complete CIIT Training Institute Example
Suppose CIIT Training Institute has a course details component. When the selected course changes, the component receives the new value through @Input.
The component can use lifecycle hooks to initialize course information, respond to changes, work with its view, and perform cleanup.
import {
Component,
Input,
OnChanges,
OnInit,
AfterViewInit,
OnDestroy,
SimpleChanges
} from '@angular/core';
@Component({
selector: 'app-course-details',
templateUrl: './course-details.component.html'
})
export class CourseDetailsComponent
implements
OnChanges,
OnInit,
AfterViewInit,
OnDestroy {
@Input() courseName = '';
ngOnChanges(changes: SimpleChanges) {
console.log('Course changed');
}
ngOnInit() {
console.log('Component initialized');
}
ngAfterViewInit() {
console.log('View initialized');
}
ngOnDestroy() {
console.log('Component destroyed');
}
}
Lifecycle Execution Flow
Important Points
Use @OnInit for component initialization tasks.
Use @OnChanges when an @Input property changes.
Use @AfterViewInit when the component view has been initialized.
Use @OnDestroy for cleanup operations before the component is removed.
Lifecycle Hooks Best Practices
- Use the appropriate lifecycle hook for each task.
- Keep initialization logic inside @OnInit.
- Use @OnChanges when responding to Input changes.
- Use @AfterViewInit only when the initialized view is required.
- Perform cleanup operations inside @OnDestroy.
- Keep lifecycle methods short and focused.
Summary
Angular lifecycle hooks allow developers to execute code at important stages of a component's lifecycle. @OnChanges handles Input changes, @OnInit handles initialization, @AfterViewInit handles view initialization, and @OnDestroy is used for cleanup before a component is destroyed.