Last updated Oct 30, 2022

Understanding the Difference between Constructor and ngOnInit in Angular 20

Learn when to use constructor vs ngOnInit in Angular 20.

Understanding the Difference between Constructor and ngOnInit in Angular 20

In this article, we will see the difference between Constructor and ngOnInit in Angular.

When working with Angular, you may have come across two important functions - the constructor and ngOnInit. While both of these functions play a crucial role in an Angular application, they are used for different purposes. In this blog post, we will discuss the difference between constructor and ngOnInit in Angular 15, and when to use each.

Constructor in Angular

The constructor function in Angular is a special function that is executed whenever a new instance of a component is created. This function is called only once during the lifecycle of a component, and it is used for initializing the properties of the component.

The Constructor is not actually part of Angular, it is a feature of TypeScript class, which is called when the class is instantiated.

Constructor is used to create a new instance of the class.

The constructor will be automatically invoked by the JavaSscprt engine.

In Angular, the constructor is used for injecting dependencies into the component class.

While writing the code try to keep the constructor as simple as possible, so Unit Testing can be very easy if the constructor logic is simple.

Here is an example of a constructor function in Angular:

export class AppComponent {
  title = 'My Angular App';

  constructor() {
    console.log('constructor');
  }
}

NgOnInit

The ngOnInit function in Angular is another important function that is called after the constructor function. This function is part of the Angular component lifecycle and is called every time the component is initialized. It is used for initializing the data that the component needs to display.

Every component we create has a life cycle managed by Angular.

The NgOnInt is a life cycle hook managed by Angular.

Whenever you create a new component using angular-CLI ngOnint is added by default.

NgOnint will be called after the constrcor execution and after first NgOnchanges

NgOnInit() is called by Angular to indicate that Angular is done with initializing the component.

The NgonInit is called after the constructor is executed. In Constructor Angular initializes and resolve all class members so in NgonInit you can initialize work and logic the component.

Here is an example of an ngOnInit function in Angular:

export class AppComponent implements OnInit {
  title = 'My Angular App';

  constructor() {
    console.log('constructor');
  }

  ngOnInit() {
    console.log('ngOnInit');
  }
}

Difference between Constructor and ngOnInit

The main difference between the constructor and ngOnInit functions in Angular is that the constructor function is called only once during the initialization of a component, while the ngOnInit function is called every time the component is initialized.

The constructor function is used for initializing the properties of a component, while the ngOnInit function is used for initializing the data that the component needs to display.

When to use Constructor and ngOnInit

Use the constructor function to initialize the properties of a component. This function is called only once during the lifecycle of a component.

Use the ngOnInit function to initialize the data that the component needs to display. This function is called every time the component is initialized.

Conclusion

Both the constructor and ngOnInit functions are important in Angular, and they are used for different purposes.

The constructor function is used for initializing the properties of a component, while the ngOnInit function is used for initializing the data that the component needs to display. Understanding the difference between these two functions is essential for building robust and efficient Angular applications.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.