Difference between Constructor and ngOnInit
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Angular, the constructor and ngOnInit both run early in a component’s life, but they are not interchangeable. The constructor belongs to the TypeScript class itself and is mainly for dependency injection and basic object setup, while ngOnInit is an Angular lifecycle hook that runs after Angular has initialized the component’s input-bound data.
What the constructor is for
The constructor is standard TypeScript and runs when the class instance is created.
This is the right place for:
- dependency injection
- simple field setup
- class-level initialization that does not depend on Angular inputs or rendered view state
It is not the best place for initialization that depends on Angular bindings.
What ngOnInit is for
ngOnInit is part of Angular’s component lifecycle.
This is the right place for:
- component initialization logic
- fetching or preparing data for display
- work that depends on Angular having set input values
ngOnInit runs once after Angular has initialized the component’s input properties.
Why they are different
The key difference is ownership and timing.
- the constructor is part of class instantiation
- '
ngOnInitis part of Angular lifecycle management'
That means the constructor can run before Angular-specific state is ready, while ngOnInit runs after Angular has done more setup work.
So when people ask which one to use, the answer is usually not “either.” It is “use each for its own job.”
A common example with @Input
Suppose a component receives data from its parent.
In practice, ngOnInit is the safer place for logic that depends on the bound input being ready.
Keep constructors simple
A good Angular rule is to keep constructors light. If you pack too much logic into them, components become harder to test and lifecycle assumptions become less clear.
That guideline also makes refactoring easier, because data-loading and binding-related logic stays in the lifecycle phase that Angular actually manages.
Constructors should mostly declare dependencies and do minimal setup. Let lifecycle hooks handle Angular-aware behavior.
What about DOM access
Neither constructor nor ngOnInit is the right place for some kinds of DOM-dependent logic. If you need child view elements or template references, later hooks such as ngAfterViewInit may be more appropriate.
That is another reason lifecycle hooks exist in layers: not all initialization happens at the same stage.
Common Pitfalls
A common mistake is putting Angular-dependent initialization in the constructor just because it runs first.
Another mistake is assuming the constructor and ngOnInit are redundant. They belong to different systems and different phases.
A third mistake is using ngOnInit for dependency injection, which is unnecessary because Angular already injects dependencies through the constructor.
Summary
- The constructor is for class instantiation and dependency injection.
- '
ngOnInitis for Angular component initialization logic.' - Use the constructor for simple setup, not input-dependent behavior.
- Use
ngOnInitwhen logic depends on Angular having initialized bindings. - For later view-dependent work, use the appropriate later lifecycle hook instead.

