Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an angular component class get instantiated?

I'm new to Angular and I see that we use classes for components. But I don't see anywhere instantiating classes and making object instances from them by using the new keyword for example. We just define a class and use it. Some classes does not even have a constructor, like the app.component.ts, only title property inside. How come? In one article I was reading it says

Constructor is invoked when Angular creates a component or directive by calling new on the class.

So where is new being called? Is it all happening behind the scenes? So angular instantiate classes and make objects out of them on the compile time, when compiling from TS to JS? If so, where can I find JS source code for this to see real example of it?

This is really confusing for me and I really want to understand how Angular 2+ works under the hood. Some links to more articles about it would be nice too, to see all the javascript logic and magic for this framework.

Thanks.

like image 985
Limpuls Avatar asked Dec 11 '25 11:12

Limpuls


1 Answers

class A {
  title = 'foo';
}

It is a Field declarations and it's the same as

class A {
  constructor(){
    this.title = 'foo';
  }
}

New instance created when angular runtime decide to show component.

like image 184
Buggy Avatar answered Dec 13 '25 10:12

Buggy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!