The aim of this tutorial is to give you a comprehensive understanding of Angular's component lifecycle, its different phases, and how we can utilize lifecycle hooks to manage various aspects of a component's lifecycle.
By the end of this tutorial, you will:
To follow this tutorial, you should have a basic understanding of:
In Angular, every component goes through a lifecycle, a series of phases from its initialization to its eventual destruction. Angular provides lifecycle hooks, specific methods that allow us to tap into different phases of this lifecycle.
Below are the lifecycle hooks in the order they are typically called:
ngOnChanges()
: This method is called when Angular sets or resets data-bound input properties. ngOnInit()
: This is called once, after the first ngOnChanges()
. It's used for initialization work.ngDoCheck()
: Detects and acts upon changes that Angular can't or won't detect on its own.ngAfterContentInit()
: Called once after Angular projects external content into the component's view.ngAfterContentChecked()
: Called after Angular checks the content projected into the component.ngAfterViewInit()
: Called once after Angular initializes the component's views and child views.ngAfterViewChecked()
: Called after Angular checks the component's views and child views.ngOnDestroy()
: Called just before Angular destroys the directive/component.Let's understand these hooks with an example. We'll create a simple component and apply these hooks.
import { Component, OnInit, OnChanges, DoCheck, AfterContentInit, AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy } from '@angular/core';
@Component({
selector: 'app-sample',
template: `<p>Sample Component</p>`
})
export class SampleComponent implements OnInit, OnChanges, DoCheck, AfterContentInit, AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy {
constructor() { }
ngOnChanges() {
console.log('ngOnChanges called');
}
ngOnInit() {
console.log('ngOnInit called');
}
ngDoCheck() {
console.log('ngDoCheck called');
}
ngAfterContentInit() {
console.log('ngAfterContentInit called');
}
ngAfterContentChecked() {
console.log('ngAfterContentChecked called');
}
ngAfterViewInit() {
console.log('ngAfterViewInit called');
}
ngAfterViewChecked() {
console.log('ngAfterViewChecked called');
}
ngOnDestroy() {
console.log('ngOnDestroy called');
}
}
In this example, we have a simple component that implements all lifecycle hooks. Each hook logs a message to the console when it's called.
This tutorial covered the Angular component lifecycle, lifecycle hooks, and how to use them. We created a simple Angular component and applied all lifecycle hooks. Remember, not every component needs to implement every lifecycle hook. You should choose the hooks that are most relevant to your needs.
Let's test your understanding with a couple of exercises:
Exercise 1: Create a component that implements only ngOnInit
and ngOnDestroy
lifecycle hooks. Log a message to the console when each hook is called. What did you observe?
Exercise 2: Create a component with an input property. Implement ngOnChanges
and log the changes to the console. What happens when you change the input property's value?
Remember, practice is key. The more you work with these concepts, the better you'll understand them. Happy coding!
Here are the solutions for the exercises:
Solution 1: When you run the component, you should see the log message from ngOnInit
. When the component is destroyed, you should see the log message from ngOnDestroy
.
Solution 2: Whenever the input property's value changes, ngOnChanges
is called and the changes are logged to the console.
Here are a few resources for further reading: