Understanding Angular Component Lifecycle

Tutorial 1 of 5

1. Introduction

Goal

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.

Learning Outcomes

By the end of this tutorial, you will:

  • Understand what Angular component lifecycle is
  • Know the different lifecycle hooks in Angular
  • Understand how and when to use these lifecycle hooks
  • Have practical experience with examples and exercises

Prerequisites

To follow this tutorial, you should have a basic understanding of:

  • JavaScript (ES6+)
  • TypeScript
  • Angular basics (components, directives, services)

2. Step-by-Step Guide

Understanding Angular Component Lifecycle

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.

Lifecycle Hooks

Below are the lifecycle hooks in the order they are typically called:

  1. ngOnChanges(): This method is called when Angular sets or resets data-bound input properties.
  2. ngOnInit(): This is called once, after the first ngOnChanges(). It's used for initialization work.
  3. ngDoCheck(): Detects and acts upon changes that Angular can't or won't detect on its own.
  4. ngAfterContentInit(): Called once after Angular projects external content into the component's view.
  5. ngAfterContentChecked(): Called after Angular checks the content projected into the component.
  6. ngAfterViewInit(): Called once after Angular initializes the component's views and child views.
  7. ngAfterViewChecked(): Called after Angular checks the component's views and child views.
  8. ngOnDestroy(): Called just before Angular destroys the directive/component.

3. Code Examples

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.

4. Summary

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.

5. Practice Exercises

Let's test your understanding with a couple of exercises:

  1. 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?

  2. 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!

Solutions

Here are the solutions for the exercises:

  1. 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.

  2. Solution 2: Whenever the input property's value changes, ngOnChanges is called and the changes are logged to the console.

Additional Resources

Here are a few resources for further reading: