Angular manages creation, initialization, rendering, data-binding and removal of component and its child components. Angular offers life cycle hooks that provide more control over these key life moments and the ability to act when they occur. Angular executes the constructor first then only execution of all other life cycle hook methods occurs explicitly in following order:
ngOnChanges called after set/changes in Input properties with SimpleChanges object as parameter having current and previous property values. Angular only calls ngOnChanges when the value of the property changes, it doesn't report changes in inner properties as change in property doesn't change in object reference in JavaScript.
This life cycle hook runs only once when component is initialized. This is the perfect place for fetching data and component initializations.
This life cycle hook called during every change detection run, frequency of calling this hooks is very high. We can implement our own change detection algorithm for the given component.
This will be called just before Angular destroys the component.
This life cycle hook called after external content projection into view(using ng-content).
ngAfterContentChecked hook is called after Angular checks the content projected into the component. It runs initially after the ngAfterContentInit finishes and every time the change detection runs.
ngAfterContentInit called after component and all child components view has been initialized. It runs only during first change detection cycle.
ngAfterViewChecked called after every time the view of the given component has been checked by the change detection mechanism.
The steps to use life cycle hooks are as follows:
import { Component, OnInit, OnChanges, SimpleChanges, Input,
DoCheck,
AfterContentInit,
AfterContentChecked,
AfterViewInit,
AfterViewChecked,
OnDestroy } from '@angular/core';