Difference between Constructor and ngOnInit in Angular

Overview of two Angular methods with code examples and explanations
30 August 2017   1861

The Constructor is a default method of the class that is executed when the class is instantiated and ensures proper initialization of fields in the class and its subclasses. Angular or better DI analyzes the constructor parameters and when it creates a new instance by calling new MyClass() it tries to find providers that match the types of the constructor parameters, resolves them and passes them to the constructor like

new MyClass(someArg);

ngOnInit is a life cycle hook called by Angular2 to indicate that Angular is done creating the component.

We have to import OnInit in order to use like this (actually implementing OnInit is not mandatory but considered good practice):

import {Component, OnInit} from '@angular/core';

then to use the method of OnInit we have to implement in the class like this.

export class App implements OnInit{
  constructor(){
     //called first time before the ngOnInit()
  }

  ngOnInit(){
     //called after the constructor and called  after the first ngOnChanges() 
  }
}

Implement this interface to execute custom initialization logic after your directive's data-bound properties have been initialized. ngOnInit is called right after the directive's data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.

Mostly we use ngOnInit for all the initialization/declaration and avoid stuff to work in the constructor. The constructor should only be used to initialize class members but shouldn't do actual "work".

So you should use constructor() to setup Dependency Injection and not much else. ngOnInit() is better place to "start" - it's where/when components' bindings are resolved.

TIOBE Index for July 2018 Released

Top programming lanuages for July 2018 index released: TypeScript continues to gain popularity 
09 July 2018   204

TypeScript entered the top 50 of the TIOBE index. A month earlier it appeared for the first time in the top 100 on line 93. TS is slowly catching up on the popularity of its competitor Dart from Google and behind it by 26 positions. TIOBE attribute this success to TypeScript with the popularity of the Angular framework, the latest versions of which are written in TS.

Objective-C returned to the top ten, Haskell - in the top 50. The scripting language Lua rose from 36 to 25 places. But F #, flew to the 36th line, losing 19 positions.

TIOBE July 2018 top 20
TIOBE July 2018 top 20

Since this month, two new programming languages have been added to the rating: Chapel and Pony. They took 97 and 173 places respectively.

TIOBE Index July 2018 Trends
TIOBE Index July 2018 Trends

The popularity of JavaScript is still high. All first half of 2018, the language spent in the top 10 and lost only 2 positions in February.

It is important to note that the rating does not show the "best" programming language, but only reflects the interest of IT industry representatives. The authors of TIOBE take into account the number of specialists around the world using a certain language, the number of language courses on educational resources, analyze the popularity of the AP with the help of the search engines of Google, Bing, Wikipedia, Amazon, YouTube, Baidu and others.