Angular Lifecycle Hooks
ANGULAR LIFECYCLE HOOKS

Angular Lifecycle Hooks

Angular Lifecycle Hooks are the different phases of the component from its creation to its destruction where we can attach our functions that will occur during the occurrence of that particular phase.

A component has a lifecycle managed by Angular.

Angular creates a component, renders it , creates and renders its children, checks it when its data-bound properties change, and destroys it before removing it from the DOM.

These lifecycle hooks are the places that provide visibility into these key life events and the ability to act when they occur.

A directive also has these same set of lifecycle hooks.

These lifecycle hooks are the interfaces present in Angular core library. To attach a method to the lifecycle hook you can implement one or more of these hooks. Each interface has a single hook method whose name is the interface name prefixed with?ng.?

The sequence of the Lifecycle hooks:

ngOnChanges():

This lifecycle hook keeps track of the set and reset of the data-bound input properties. The method receives a SimpleChanges object of the current and the previous property values. It is called before ngOnInit() and whenever one or more data-bound input properties change.

ngOnInit():

It initialises the component/directive after angular first displays the data-bound properties and sets the component's/directive's input properties. It is called only once and after the first occurrence of the ngOnChanges().

ngDoCheck():

It detects and acts upon the changes that Angular can't detect on its own. It is called during every change detection run, immediately after ngOnChanges() and ngOnInit().

ngAfterContentInit():

It responds after Angular projects external content into the components view or in the view a directive is in. It is called once after the first ngDoCheck().

ngAfterContentChecked():

It responds after Angular checks the content projected into the directive/component. It is called after the ngAfterContentInit() and every subsequent ngDoCheck().

ngAfterViewInit():

It responds after Angular initialises the component's views and child views or the view that a directive is in. It is called once after the first ngAfterContentChecked().

ngAfterViewChecked():

It responds after Angular checks the component's views and child views or the view that a directive is in. It is called after the ngAfteViewInit() and every subsequent ngAfterContentChecked().

ngOnDestroy():

It is called to perform cleanup activities like unsubscribe observable and detach event handlers to avoid memory leaks. It is called just before Angular destroys the directive/component.

The syntax for the implementation of the life cycle hook interface is as follows:

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

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit, OnChanges,
 DoCheck, OnDestroy,AfterContentInit, AfterContentChecked,
 AfterViewInit, AfterViewChecked {

   ngOnChanges(){
      // some method
   }

   ngOnInit(){
      // some method
   } 

   ngDoCheck(){
      // some method
   }

   ngAfterContentInit(){
      // some method
   }

   ngAfterContentChecked(){
      // some method
   }

   ngAfterViewInit(){
      // some method
   }

   ngAfterViewChecked(){
      // some method
   }

   ngOnDestroy(){
      // some method
   }

}                                        
                           

The lifecycle hooks gives an opportunity to the developers to make their application smooth and robust.

Try out these hook's to attach your methods and events and let me know your experiences of using it. I hope it will make your coding tasks a lot more easier. Feel free to drop your suggestions and experiences with the angular lifecycle hooks.

要查看或添加评论,请登录

Nagma Bano的更多文章

  • Data Communication In Angular

    Data Communication In Angular

    Data sharing is one of the important concept to understand in Angular. How Angular communicates data from and to…

    4 条评论
  • Introduction to Data binding

    Introduction to Data binding

    Data-binding is the mechanism for coordinating the parts of a template with the parts of the component. You can achieve…

  • Modules in Angular

    Modules in Angular

    Modules are a great way to organize an application and extend it with capabilities from external libraries. NgModules…

  • Templating, Styling and Component Hierarchy in Angular

    Templating, Styling and Component Hierarchy in Angular

    In Angular, your views are defined within HTML templates. A template is a form of HTML that tells Angular how to render…

    1 条评论
  • Introduction to Components

    Introduction to Components

    The core concept of any Angular application is the component. In effect, the whole application can be modeled as a tree…

    1 条评论
  • Bootstrapping of the Angular Application

    Bootstrapping of the Angular Application

    Seeing the complexity of the Angular file structure, the first question that comes to the mind is how does it actually…

    1 条评论
  • Angular Architecture

    Angular Architecture

    The main building blocks of Angular are: Modules Components Templates, Directives, and Data Binding Services and…

    1 条评论
  • Getting Started With Angular

    Getting Started With Angular

    To start working with Angular we need to first set up the development environment. A development environment is one…

    1 条评论
  • Angular Features and Benefits

    Angular Features and Benefits

    Angular is one of the most popular modern day web frameworks available today. Because of the sheer support of Google…

  • Angular6 Vs Angular5

    Angular6 Vs Angular5

    Angular 5 Angular 5 showed up on November 1, 2017 with the promise of speed and a smaller size. The key features of…

社区洞察

其他会员也浏览了