1

If we have multiple instances of one component inside another component and we have constructor and ngOnInit() interface implemented in all of them, let's say we are just logging to the console, so will we have first constructor console.log() execution then console.log() of ngOnInit() for first instance of component and then second, third?

I'm getting all constructor console.log first then of ngOnInit()

2
  • The constructor is always the first thing called when you instaciate a component, after that, then angular starts its lifecyle angular.io/guide/lifecycle-hooks Commented Dec 28, 2022 at 12:55
  • @CaioOliveira yes I understand that part but as I said if we have multiple instances of one component, let's say directive of component B is placed 3 times in template of component A so it means 3 instances will be created of component A. Right? But shouldn't it be likes this that first instance is created, console.log of first instance in constructor logs, then console.log of its ngOnInit logs, then same for other 2 instances. I'm getting 3 constructor logs first and then 3 logs of ngOnInit. I hope this make sense! Commented Dec 28, 2022 at 13:02

1 Answer 1

2

Yes this is right. The constructor will be called as the first. After the component/child components start to init. So if you log the complete lifecycle (see the docs)

After your application instantiates a component or directive by calling its constructor, Angular calls the hook methods you have implemented at the appropriate point in the lifecycle of that instance.

The output with 3 childs as example will be:

Constructor
Constructor
Constructor
OnInit
AfterViewInit
OnInit
AfterViewInit
OnInit
AfterViewInit

Why? The constructor is a simple, standard class constructor every TypeScript class has. Angular init all components and then the first ngOnChanges fire. And that only happens afterwards.

If Angular did this for every component created, it would start affecting performance, among other things.

If you try to call ref.detectChanges(); inside a childs constructor to force a update manually, a error occur:

ERROR Error: ASSERTION ERROR: Should be run in update mode [Expected=> false == true <=Actual].

This means Angular check changes after constructor was completed. And the first constructor is the parent. And this one is just finished when all child constructors finished, too.

In this example the child log first, at the end the parent.

Constructor Child

Constructor Child

Constructor Parent - ngOnChanges()

class parent {
  constructor() {
    let c1 = new child();
    let c2 = new child();
    console.log("Constructor Parent - ngOnChanges()");
  }
}
...
class child {
  constructor() {
    console.log("Constructor Child");
  }
}

I hope my words help to understanding this better.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for clearly understanding my question. But I wanted some explanation on this part that if we have multiple instances of some component, shouldn't Angular takes care of 1 component first that is logs the constructor execution and then of ngOnInit then move to second instance which makes more sense to me. (I'm beginner so I hope you'd describe in details)
Answer updated. I hope it helps to understand Angulars architecture.
Thank you for taking the time to describe it. Will you please explain this last line? Not sure what you mean by first constructor is the parent the child cons. "And the first constructor is the parent. And this one is just finished when all child constructors finished, too."
constructor() { let t = new test2(); console.log("Constructor Parent"); } Here the "test2" constructor will be done first then it's parent constructor.
thanks for your time. One last question. Can you please refer me to some resources you use to get the deep understanding / core feature of Angular like anything else then official doc. Yes, you must have gained most of knowledge from your experience but still some resource you want to share?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.