0

I have a parent class and child class in Angular. When trying to create a constructor inside child class it throws some error:

Constructors for derived classes must contain a 'super' call.

It will be working if the code is added in ngOnInit instead of constructor. So what is the difference between these two? Both will be invoked up on instantiating the class.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
Shaju Nr
  • 338
  • 3
  • 18

1 Answers1

3

According to Angular,

OnInit is a callback method that is invoked immediately after the default change detector has checked the directive's data-bound properties for the first time, and before any of the view or content children have been checked. It is invoked only once when the directive is instantiated.

Constructor is similar to that of any class (with object oriented principles). And it's called when that class instance is created.

Also check this.

Malindu Sandaruwan
  • 1,477
  • 2
  • 13
  • 27