What is OnInit in Angular?
207
16-Dec-2025
Updated on 29-Jan-2026
Anubhav Kumar
16-Dec-2025What exactly is
OnInit?OnInitis an interface provided by Angular.ngOnInit()ngOnInit()after the component’s constructor runs and after all@Input()values are set.Why do we use
OnInit?You use
OnInitwhen you want to:@Input()valuesBasic example
Execution order
ngOnInit()Constructor vs
ngOnInit()ngOnInit()Best practice:
ngOnInit()for initialization logicExample with
@Input()When NOT to use
OnInitngOnChangesngOnDestroyngAfterViewInitSummary
OnInitis an Angular lifecycle hookngOnInit()