📖 Scenario: You are building a simple Angular component to understand the order in which Angular lifecycle hooks run. This helps you see how Angular initializes, updates, and destroys components step-by-step.
🎯 Goal: Create an Angular standalone component that logs messages in each lifecycle hook to show their execution order clearly.
📋 What You'll Learn
Create a standalone Angular component named LifecycleDemoComponent
Use Angular lifecycle hooks: ngOnInit, ngOnChanges, ngDoCheck, ngAfterContentInit, ngAfterContentChecked, ngAfterViewInit, ngAfterViewChecked, and ngOnDestroy
Log a unique message in each lifecycle hook method
Add an input property named inputValue to trigger ngOnChanges
Use the @Component decorator with standalone: true
Include a simple template that displays inputValue
💡 Why This Matters
🌍 Real World
Understanding Angular lifecycle hooks helps developers debug component behavior and optimize performance in real applications.
💼 Career
Knowing lifecycle execution order is essential for Angular developers to manage component initialization, updates, and cleanup correctly.
Progress0 / 4 steps
1
Create the Angular standalone component with input property
Create a standalone Angular component named LifecycleDemoComponent with an input property called inputValue of type string. Use the @Component decorator with standalone: true and a template that displays inputValue inside a <p> tag.
Angular
Hint
Remember to import Component and Input from @angular/core. Use @Input() to declare the input property.
2
Add ngOnInit and ngOnChanges lifecycle hooks with console logs
Add the lifecycle hook methods ngOnInit and ngOnChanges inside LifecycleDemoComponent. In each method, add a console.log statement with the messages 'ngOnInit called' and 'ngOnChanges called' respectively.
Angular
Hint
Import OnInit, OnChanges, and SimpleChanges from @angular/core. Implement the interfaces and add the methods with console logs.
3
Add remaining lifecycle hooks with console logs
Add the lifecycle hook methods ngDoCheck, ngAfterContentInit, ngAfterContentChecked, ngAfterViewInit, ngAfterViewChecked, and ngOnDestroy to LifecycleDemoComponent. In each method, add a console.log statement with messages like 'ngDoCheck called', 'ngAfterContentInit called', etc.
Angular
Hint
Import all lifecycle interfaces from @angular/core. Implement them and add each method with a console log message.
4
Add a button to change inputValue and trigger lifecycle hooks
Add a button to the component template that changes the inputValue property when clicked. This will trigger ngOnChanges and other lifecycle hooks. Update the template to include a <button> with a click event that sets inputValue to 'Updated!'.
Angular
Hint
Add a button inside the template with a click event that sets inputValue to 'Updated!'. This triggers Angular lifecycle hooks.
Practice
(1/5)
1. Which Angular lifecycle hook runs first when a component receives new input values? ngOnInit, ngOnChanges, ngAfterViewInit, or ngOnDestroy?
easy
A. ngOnChanges
B. ngOnInit
C. ngAfterViewInit
D. ngOnDestroy
Solution
Step 1: Understand lifecycle hooks on input change
When input properties change, Angular calls ngOnChanges first to react to those changes.
Step 2: Identify the first hook called on input update
ngOnInit runs only once after the first ngOnChanges, so it is not first.
Final Answer:
ngOnChanges -> Option A
Quick Check:
Input changes trigger ngOnChanges first [OK]
Hint: Remember: ngOnChanges always runs before ngOnInit on input change [OK]
Common Mistakes:
Thinking ngOnInit runs before ngOnChanges
Confusing ngAfterViewInit as first hook
Assuming ngOnDestroy runs on input change
2. Which of the following is the correct syntax to implement the ngOnDestroy lifecycle hook in an Angular component?
easy
A. destroy() { console.log('Destroyed'); }
B. onDestroy() { console.log('Destroyed'); }
C. ngOnDestroy() { console.log('Destroyed'); }
D. ngDestroy() { console.log('Destroyed'); }
Solution
Step 1: Recall Angular lifecycle hook naming
Angular lifecycle hooks have specific method names starting with 'ng', so ngOnDestroy is correct.
Step 2: Match method name exactly
Only ngOnDestroy() { console.log('Destroyed'); } uses the exact method name ngOnDestroy required by Angular.
Final Answer:
ngOnDestroy() { console.log('Destroyed'); } -> Option C
Quick Check:
Lifecycle hooks start with 'ngOn' [OK]
Hint: Lifecycle hooks always start with 'ngOn' followed by event [OK]
Common Mistakes:
Using incorrect method names like onDestroy or destroy
Missing 'ngOn' prefix
Confusing lifecycle hook names with custom methods