Angular video series - #2 Lifecycle hooks explained

Опубликовано: 29 Октябрь 2024
на канале: Code with Sloba
4,546
150

Lifecycle hooks are a special functionality in Angular that allow us to “hook into” and run code at a specific lifecycle event of a component or directive.

You can look at them as phases of component life like every living being has. They get born, they change, they grow and eventually they die.
With lifecycle hooks, we can gain better control of our application.

When do we use them?

For example if you want to fetch some data and display it in your component, you will do that inside of ngOnit lifecycle when component gets initialized, or if you need to cleanup your component, remove some subscriptions you might have to prevent memory leaks you will do that in ngOnDestory lifecycle hook, just before component gets destroyed.


0:00 Intro
0:25 When to use them?
1:00 Implementing ngOnInit
1:50 Implementing ngOnDestroy

************************************************************
Previous Video:    • Angular video series - #1 Components  
Next video:    • Angular video series - #3 Text interp...  
Full series:    • Full Angular Course  
************************************************************
Linkedin: link   / slobodan-gajic  
Portfolio: https://bobangajicsm.github.io/portfo...
Twitter:   / _slobodangajic_  
************************************************************