CHAPTER 15
Beginner
Angular Lifecycle Hooks
Updated: May 18, 2026
5 min read
# CHAPTER 15
Angular Lifecycle Hooks
1. Chapter Introduction
Every Angular component has a lifecycle — a series of stages from creation to destruction. Angular calls specific hook methods at each stage, allowing developers to run custom code at precisely the right moment. Understanding these hooks is critical for loading data correctly, responding to input changes, and preventing memory leaks.2. Learning Objectives
- Understand the Angular component lifecycle sequence.
-
Use
ngOnInitfor initialization logic.
-
Use
ngOnChangesto react to@Inputchanges.
-
Use
ngOnDestroyfor cleanup (unsubscribing Observables).
-
Implement other hooks:
ngAfterViewInit,ngDoCheck.
3. The Lifecycle Sequence
text
4. ngOnInit — The Most Important Hook
Use ngOnInit to fetch data, initialize subscriptions, and set up the component. Never put heavy logic in the constructor — the constructor should only receive injected services.
typescript
5. ngOnChanges — Reacting to @Input Changes
typescript
6. ngOnDestroy — Preventing Memory Leaks
When a component is removed, any active Observable subscriptions continue running in memory unless explicitly cancelled. This is a memory leak!
typescript
7. ngAfterViewInit — After View Renders
typescript
8. Common Mistakes
-
HTTP calls in the constructor: The Angular DI system hasn't fully initialized when the constructor runs. Always put initialization logic in
ngOnInit.
-
Not unsubscribing: The most common Angular memory leak. If you subscribe in
ngOnInit, always unsubscribe inngOnDestroy(or use theasyncpipe which handles this automatically).
9. Best Practice: The takeUntilDestroyed Operator (Angular 16+)
typescript
10. MCQs with Answers
Question 1
Which lifecycle hook fires ONCE after a component's properties have been initialized?
Question 2
Which hook should you use to detect and respond to changes in @Input properties?
Question 3
Which hook is the correct place to unsubscribe from Observables to prevent memory leaks?
Question 4
Why is it bad practice to make HTTP calls inside the constructor?
Question 5
What does ngAfterViewInit guarantee?
Question 6
What causes a memory leak in Angular?
Question 7
What does the SimpleChanges object contain in ngOnChanges?
Question 8
In what order do the first three lifecycle hooks execute?
Question 9
What modern Angular operator automatically handles unsubscription?
Question 10
What interface should a component implement if it uses ngOnInit?
11. Interview Questions
-
Q: What is the difference between the constructor and
ngOnInit?
- Q: How do you prevent memory leaks caused by Observable subscriptions in Angular?
12. Summary
Angular's lifecycle hooks give developers precise control over a component's existence.ngOnInit is the correct place for initialization work, ngOnChanges responds to parent data updates, and ngOnDestroy is the cleanup guardian preventing memory leaks. Mastering these hooks is a sign of an experienced Angular developer.
13. Next Chapter Recommendation
Every Angular component belongs to a module. In Chapter 16: Angular Modules, we exploreNgModules, feature modules, shared modules, and how modules control what gets imported and exported in your application.