In this article, we will discuss the life cycle of components. Since in Angular, Component is the main building block the application, so it is very important for us to understand the life cycle processing steps of the components so that we can implement that in our applications.

Life Cycle Method

In Angular, every component has a life-cycle, a number of different stages it goes through from initializing to destroying. There are 8 different stages in the component lifecycle. Every stage is called life cycle hook events. So, we can use these hook events in different phases of our application to obtains fine controls on the components. Since a component is a typescript class, for that reason every component must have a constructor method. The constructor of the component class executes first before the execution of any other life cycle hook event. If we need to inject any dependencies into the component, then the constructor is the best place to inject that dependency. After executing the constructor, Angular executes its life cycle hook methods in a specific order.

These stages are mainly divided into two phases – one is linked to the component itself and another is linked to the children of that component.

  • ngOnChanges
    This event executes every time when a value of an input control within the component has been changed. Actually, this event fires first when a value of a bound property has been changed. It always receives a changed data map, containing the current and previous value of the bound property wrapped in a SimpleChange.
    1. {"name":{"previousValue":"","currentValue":"Amit"}}
In this example above, one value change has been detected to the input property name. The value of this property has been changed from an empty string to a string value “Amit”.

Interfaces

As per the above discussion, we can just define the life cycle hook methods directly on the component class, but we can use the advantage of the interface. Since each of these lifecycle hook methods has an associated typescript interface, the name of those interfaces is the same name just without the ng prefix like ngOnInit has an interface called OnInit. Each interface defines just one life cycle hook method. One more important note that the browser-based TypeScript compiler does not raise a compilation error when we don’t implement interface functions in our class. But, in the compiling time of the typescript code, it will throw an error.

  1. import {
  2. OnChanges,
  3. OnInit,
  4. DoCheck,
  5. AfterContentInit,
  6. AfterContentChecked,
  7. AfterViewInit,
  8. AfterViewChecked,
  9. OnDestroy
  10. } from '@angular/core';
  11. class SampleComponent implements
  12. OnChanges,
  13. OnInit,
  14. DoCheck,
  15. AfterContentInit,
  16. AfterContentChecked,
  17. AfterViewInit,
  18. AfterViewChecked,
  19. OnDestroy {
  20. ...
  21. }
Sample code of app.component.lifecycle.ts
  1. import { Component } from '@angular/core';
  2. @Component({
  3. moduleId: module.id,
  4. selector: 'lifecycle-sample',
  5. templateUrl: 'app.component.lifecycle.html'
  6. })
  7. export class LifeCycleComponent {
  8. data:number=100;
  9. constructor() {
  10. console.log(`new - data is ${this.data}`);
  11. }
  12. ngOnChanges() {
  13. console.log(`ngOnChanges - data is ${this.data}`);
  14. }
  15. ngOnInit() {
  16. console.log(`ngOnInit - data is ${this.data}`);
  17. }
  18. ngDoCheck() {
  19. console.log("ngDoCheck")
  20. }
  21. ngAfterContentInit() {
  22. console.log("ngAfterContentInit");
  23. }
  24. ngAfterContentChecked() {
  25. console.log("ngAfterContentChecked");
  26. }
  27. ngAfterViewInit() {
  28. console.log("ngAfterViewInit");
  29. }
  30. ngAfterViewChecked() {
  31. console.log("ngAfterViewChecked");
  32. }
  33. ngOnDestroy() {
  34. console.log("ngOnDestroy");
  35. }
  36. fnAddNumber():void{
  37. this.data+=100;
  38. }
  39. deleteNumber():void{
  40. this.data -=10;
  41. }
  42. }
Sample code of app.component.lifecycle.html
  1. <span class="setup">Given Number</span>
  2. <h1 class="punchline">{{ data }}</h1>
  3. <button type="button" class="btn btn-success"
  4. (click)="fnAddNumber()">Add NUmber
  5. </button>
  6. <button type="button"
  7. class="btn btn-danger"
  8. (click)="deleteNumber()">Clear Number
  9. </button>
Output Result