📖 Scenario: You are building a simple Angular component that shows a message and tracks how many times it has been initialized. This helps understand how Angular lifecycle hooks work in real apps.
🎯 Goal: Create an Angular standalone component that uses the OnInit lifecycle hook to count how many times the component has been initialized and display this count in the template.
📋 What You'll Learn
Create a standalone Angular component named InitCounterComponent
Add a property initCount initialized to 0
Implement the OnInit interface and its ngOnInit() method
Increment initCount inside ngOnInit()
Display the text Component initialized X times in the template, where X is initCount
💡 Why This Matters
🌍 Real World
Understanding lifecycle hooks helps developers manage component setup and cleanup in real Angular apps, improving performance and user experience.
💼 Career
Knowing lifecycle hooks is essential for Angular developers to write maintainable, efficient components that behave correctly during their lifecycle.
Progress0 / 4 steps
1
Create the component with initial count
Create a standalone Angular component named InitCounterComponent with a property initCount set to 0. Use the @Component decorator with selector set to 'app-init-counter' and a simple template showing initCount inside a <p> tag.
Angular
Hint
Use @Component with standalone: true and define initCount as a number property set to 0.
2
Add OnInit interface and import
Import OnInit from @angular/core and make InitCounterComponent implement OnInit. Add the ngOnInit() method with an empty body.
Angular
Hint
Remember to import OnInit and add implements OnInit after the class name.
3
Increment count inside ngOnInit
Inside the ngOnInit() method, increment the initCount property by 1 using this.initCount++.
Angular
Hint
Use this.initCount++ inside ngOnInit() to increase the count.
4
Complete template to show initialization count
Ensure the component template displays the text Component initialized X times where X is the value of initCount. Update the template property in the @Component decorator accordingly.
Angular
Hint
Update the template string to exactly show Component initialized {{ initCount }} times.
Practice
(1/5)
1. Why are lifecycle hooks important in Angular components?
easy
A. They make components load faster by default.
B. They automatically style your components.
C. They replace the need for services.
D. They let you run code at specific times during a component's life.
Solution
Step 1: Understand what lifecycle hooks do
Lifecycle hooks allow you to run code at key moments like creation, update, or destruction of a component.
Step 2: Identify the main benefit
This helps manage resources and keep the app efficient by running code only when needed.
Final Answer:
They let you run code at specific times during a component's life. -> Option D
Quick Check:
Lifecycle hooks = run code at key moments [OK]
Hint: Lifecycle hooks run code at key moments [OK]
Common Mistakes:
Thinking lifecycle hooks style components
Believing hooks replace services
Assuming hooks speed up loading automatically
2. Which of the following is the correct syntax to implement the ngOnInit lifecycle hook in an Angular component?
easy
A. ngOnInit() { console.log('Init'); }
B. onInit() { console.log('Init'); }
C. nginit() { console.log('Init'); }
D. init() { console.log('Init'); }
Solution
Step 1: Recall the exact lifecycle hook name
The correct lifecycle hook method name is ngOnInit with exact casing.
Step 2: Match the syntax
The method should be declared as ngOnInit() { ... } inside the component class.
Final Answer:
ngOnInit() { console.log('Init'); } -> Option A
Quick Check:
Correct hook name and syntax = ngOnInit() { console.log('Init'); } [OK]
Hint: Lifecycle hooks start with 'ng' and use camelCase [OK]
Common Mistakes:
Using wrong method names like onInit or init
Incorrect casing like nginit
Missing 'ng' prefix in method name
3. Given this Angular component code snippet, what will be logged to the console when the component is created?
A. ngOnInit should not be implemented without a constructor.
B. ngOnDestroy must return a value.
C. Calling loadData() inside ngOnDestroy is incorrect because it runs when component is destroyed.
D. loadData() cannot be called inside lifecycle hooks.
Solution
Step 1: Understand purpose of ngOnDestroy
ngOnDestroy runs when the component is about to be removed. It is used to clean up resources, not to load data.
Step 2: Identify misuse of loadData()
Calling loadData() during destruction is illogical because loading data is for initialization, not cleanup.
Final Answer:
Calling loadData() inside ngOnDestroy is incorrect because it runs when component is destroyed. -> Option C
Quick Check:
ngOnDestroy is for cleanup, not loading data [OK]
Hint: Use ngOnDestroy only for cleanup tasks [OK]
Common Mistakes:
Thinking ngOnDestroy needs a return value
Believing lifecycle hooks can't call methods
Assuming constructor is required with ngOnInit
5. You want to fetch user data only once when a component loads and clean up a subscription when the component is destroyed. Which lifecycle hooks should you use together?
hard
A. ngOnChanges to fetch data, ngDoCheck to clean up subscription
B. ngOnInit to fetch data, ngOnDestroy to clean up subscription
C. constructor to fetch data, ngAfterViewInit to clean up subscription
D. ngAfterContentInit to fetch data, ngAfterContentChecked to clean up subscription
Solution
Step 1: Identify hook for initial data fetch
Use ngOnInit to run code once after component creation, perfect for fetching data.
Step 2: Identify hook for cleanup
Use ngOnDestroy to clean up resources like subscriptions before component is removed.
Final Answer:
ngOnInit to fetch data, ngOnDestroy to clean up subscription -> Option B
Quick Check:
Init = fetch, Destroy = cleanup [OK]
Hint: Fetch in ngOnInit, clean in ngOnDestroy [OK]
Common Mistakes:
Using constructor for async data fetch
Confusing ngAfterViewInit with cleanup
Using ngOnChanges for one-time fetch
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.