OnInit Interfaces In Angular

Introduction

 
In this article, we are going to see what is ngOninit() method, when and why to use ngOnInit() method.
 
Prerequisites
  • HTML, CSS, and JS
  • Basics of TypeScript.
  1. import { Component, OnInit } from '@angular/core';  
  2. import { Http } from '@angular/http';  
  3.   
  4. @Component({  
  5.   selector: 'app-posts',  
  6.   templateUrl: './posts.component.html',  
  7.   styleUrls: ['./posts.component.css']  
  8. })  
  9. export class PostsComponent implements OnInit {    
  10.  posts: any[];  
  11.  private url = 'http://jsonplaceholder.typicode.com/posts';  
  12.   constructor(private http: Http)   
  13.   {  
  14.     http.get(this.url)  
  15.     .subscribe(response=> {  
  16.       console.log(response.json());  
  17.       this.posts = response.json();  
  18.     });  
  19.   }  
  20.   
  21.   ngOnInit() {  
  22.   }  
  23.  }  
As a best practice we should have a constructor of very small and light weight. We should not perform expensive operations like calling of server.
 
Then when can we call it?
 
Components in Angular have  lifecycle hooks. There are special methods that we can add to Angular, and Angular  will call these methods at specific times during the lifecycle of the component.
 

Lifecycle Hooks

 
When Angular
  1. Creates a Component
  2. Renders it
  3. Creates and renders its children
  4. Destroys a component
These are lifecycle events. Angular will call specific methods if they are defined. One of its methods is  called ngOnInit.
 

OnInit interface

 
Defined under @angular/core library,
  1. Interface OnInit {  
  2.    ngOnInit(): void  
  3. }  
This interface declares a method called ngOnInit which takes no arguments and returns void. This is the method that Angular calls when it initializes the component.
 
OnInit is an interface that refers to the lifecycle hook. There are multiple lifecycle hooks in Angular:
  1. OnInit
  2. OnChanges
  3. DoCheck
  4. AfterContentInit
Each of these interfaces declares a method with the same name prefix with ng . So for OnInit we have a method called ngOnInit.
 

OnInit

 
Technically we don’t need to implement OnInit interface on the top as long as we have a method called ngOnInit() defined in our class. Angular will automatically call this when it initializes our component but we use implement OnInt interface to add compile time checking, so when we define implement OnInit interface typescript ensures that we have a method called ngOnInit.
 
So the lesson is, o not call the http services inside constructors. If you want initialization then do that in ngOnInit method.
 
EG,
  1. import { Component, OnInit } from '@angular/core';  
  2. import { Http } from '@angular/http';  
  3.   
  4. @Component({  
  5.   selector: 'app-posts',  
  6.   templateUrl: './posts.component.html',  
  7.   styleUrls: ['./posts.component.css']  
  8. })  
  9. export class PostsComponent implements OnInit {    
  10.  posts: any[];  
  11.  private url = 'http://jsonplaceholder.typicode.com/posts';  
  12.   constructor(private http: Http)   
  13.   {  
  14.       
  15.   }  
  16.   
  17.   ngOnInit() {  
  18.     this.http.get(this.url)  
  19.     .subscribe(response=> {  
  20.     console.log(response.json());  
  21.       this.posts = response.json();  
  22.     });  
  23.   }  
  24.  }