Mastering the Component Lifecycle in Angular

Photo by Boris Smokrovic on Unsplash

Mastering the Component Lifecycle in Angular

Angular Jan 6, 2023
💡
In this tutorial, we will learn about the component lifecycle in Angular and the various lifecycle hooks that are available. We will explore the sequence of phases that a component goes through from initialization to destruction and understand how to tap into and perform certain actions at specific points in this lifecycle.

In Angular, a component goes through a lifecycle of changes from initialization to destruction. The Angular framework provides several lifecycle hooks that allow you to tap into and perform certain actions at specific points in this lifecycle.

Lifecycle Hooks

There are several lifecycle hooks that you can use in Angular components:

  • ngOnChanges: called when an input binding value changes.
  • ngOnInit: called after the first ngOnChanges hook.
  • ngDoCheck: called during every change detection run.
  • ngAfterContentInit: called after the component's content has been initialized.
  • ngAfterContentChecked: called after the component's content has been checked.
  • ngAfterViewInit: called after the component's view (and child views) has been initialized.
  • ngAfterViewChecked: called after the component's view (and child views) has been checked.
  • ngOnDestroy: called just before the component is destroyed.

Here is an example of a component that uses some of these lifecycle hooks:

import {
  Component,
  OnChanges,
  OnInit,
  DoCheck,
  AfterContentInit,
  AfterContentChecked,
  AfterViewInit,
  AfterViewChecked,
  OnDestroy
} from '@angular/core';

@Component({
  selector: 'app-lifecycle',
  template: `
    <p>Lifecycle Status: {{ status }}</p>
  `
})
export class LifecycleComponent implements OnChanges, OnInit, DoCheck, AfterContentInit, AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy {
  status: string;

  ngOnChanges() {
    this.status = 'ngOnChanges';
  }

  ngOnInit() {
    this.status = 'ngOnInit';
  }

  ngDoCheck() {
    this.status = 'ngDoCheck';
  }

  ngAfterContentInit() {
    this.status = 'ngAfterContentInit';
  }

  ngAfterContentChecked() {
    this.status = 'ngAfterContentChecked';
  }

  ngAfterViewInit() {
    this.status = 'ngAfterViewInit';
  }

  ngAfterViewChecked() {
    this.status = 'ngAfterViewChecked';
  }

  ngOnDestroy() {
    this.status = 'ngOnDestroy';
  }
}

In this example, the LifecycleComponent has a status property that is displayed in the template. Each lifecycle hook updates the value of status to reflect the current phase of the component's lifecycle.

Lifecycle Sequence

The component lifecycle proceeds through the following sequence of phases:

  1. Initialization:
  • ngOnChanges
  • ngOnInit
  1. Change detection:
  • ngDoCheck
  • ngAfterContentInit
  • ngAfterContentChecked
  • ngAfterViewInit
  • ngAfterViewChecked
  1. Destruction:
  • ngOnDestroy

During initialization, the component's input bindings are initialized and the ngOnInit hook is called. This is a good place to perform initialization tasks such as setting up a subscription or making an HTTP request.

After initialization, the component enters the change detection phase. Angular runs change detection on all components periodically to check for any changes that need to be reflected in the view. During this phase, the ngDoCheck hook is called, which allows you to perform your own change detection. The ngAfterContentInit and ngAfterContentChecked hooks are called after the component's content (transcluded content) has been initialized and checked, respectively. The ngAfterViewInit and ngAfterViewChecked hooks are called after the component's view (and child views) has been initialized and checked, respectively.

Finally, when the component is about to be destroyed, the ngOnDestroy hook is called. This is a good place to perform cleanup tasks such as unsubscribing from observables or canceling any pending HTTP requests.

Wrapping Up

In this tutorial, we learned about the component lifecycle in Angular and the various lifecycle hooks that are available. We also saw the sequence of phases that a component goes through from initialization to destruction. Understanding the component lifecycle is crucial for building robust and performant Angular applications.

I hope this article helps!

Tags

Anurag Deep

Logical by Mind, Creative by Heart