Learn Angular 19 : Master ngOnInit() Life cycle hook method | Real World Example | Episode 8
In this video, we will discuss about the ngOnInit() life cycle hook method in angular 19
ngOnInit() is a lifecycle hook in Angular that runs after the component is constructed and its inputs are initialized, but before the view is rendered.
It's defined in the OnInit interface from @angular/core.
When is ngOnInit() called?
It is called once after the component’s:
Constructor runs
@Input() properties are bound
Angular has set up the component
Use Case. Why use ngOnInit()
Fetch data from API Ensures component is ready before making HTTP calls
Subscribe to services Good point to set up Observables or Subjects
Perform setup logic. Load defaults, initialize states, etc.
Call methods using @Input. Because the @Input() values are ready
constructor(). ngOnInit()
Used to inject dependencies. Used to run logic after bindings
Called before bindings Called after bindings
Don’t use for heavy logic Safe to run initialization logic
#angular10 #angular #angular16 #angular_developer