Angular FAQ: Top Questions
43. What is the difference between ngOnInit and constructor?
The constructor
is used for dependency injection and class initialization, while ngOnInit
is a lifecycle hook used for component initialization logic after inputs are set.
constructor
: Called when the class is instantiated.ngOnInit
: Called after constructor and input properties are bound.
constructor(private service: DataService) {}
ngOnInit() {
this.service.loadData();
}