An Observable is a way to handle asynchronous data streams. It lets components listen to data changes over time, like waiting for user input or server responses.
Click to reveal answer
beginner
Why should you unsubscribe from Observables in Angular components?
Unsubscribing prevents memory leaks by stopping the component from listening to data after it is destroyed. This keeps the app fast and stable.
Click to reveal answer
beginner
Which Angular lifecycle hook is best for unsubscribing from Observables?
The ngOnDestroy() hook is used to clean up subscriptions when the component is about to be removed from the screen.
Click to reveal answer
intermediate
How does the async pipe help with Observables in templates?
The async pipe automatically subscribes to an Observable and updates the view with new data. It also unsubscribes when the component is destroyed, so you don’t have to do it manually.
Click to reveal answer
intermediate
What happens if you forget to unsubscribe from an Observable in Angular?
The Observable keeps running and holding resources, causing memory leaks and possibly slowing down or crashing the app over time.
Click to reveal answer
Which lifecycle hook is used to clean up subscriptions in Angular components?
AngDoCheck
BngOnInit
CngAfterViewInit
DngOnDestroy
✗ Incorrect
ngOnDestroy is called just before the component is removed, making it the right place to unsubscribe.
What does the async pipe do in Angular templates?
AManually triggers change detection
BAutomatically subscribes and unsubscribes from Observables
CConverts Observables to Promises
DPrevents Observables from emitting values
✗ Incorrect
The async pipe handles subscription and unsubscription automatically, updating the view with new data.
Why is unsubscribing from Observables important?
ATo avoid memory leaks and improve app performance
BTo speed up the Observable emissions
CTo convert Observables to synchronous calls
DTo enable two-way data binding
✗ Incorrect
Unsubscribing stops the Observable from running when no longer needed, preventing memory leaks.
Which of these is NOT a way to manage Observable subscriptions in Angular?
AIgnoring subscriptions and letting Angular handle it
BManually unsubscribing in ngOnDestroy
CUsing async pipe in templates
DUsing takeUntil operator with a notifier
✗ Incorrect
Angular does not automatically unsubscribe for you unless you use async pipe or operators; ignoring subscriptions can cause leaks.
What is a common pattern to unsubscribe from multiple Observables in ngOnDestroy?
AUsing ngOnInit to unsubscribe
BCalling unsubscribe on each Observable separately
CUsing a Subject as a notifier with takeUntil
DUsing setTimeout to delay unsubscription
✗ Incorrect
Using a Subject with takeUntil lets you unsubscribe from many Observables at once when the notifier emits.
Explain how Observables fit into the Angular component lifecycle and why managing subscriptions is important.
Think about what happens when a component is removed and how Observables keep running if not stopped.
You got /4 concepts.
Describe how the async pipe simplifies working with Observables in Angular templates.
Consider how Angular templates can react to data changes without extra code.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to subscribe to an Observable inside the ngOnInit lifecycle hook in an Angular component?
easy
A. To start listening to data streams when the component is initialized
B. To stop listening to data streams when the component is destroyed
C. To create a new Observable instance
D. To update the component's template directly
Solution
Step 1: Understand ngOnInit role
ngOnInit runs once when the component is created, ideal to start tasks like subscribing to Observables.
Step 2: Purpose of subscribing in ngOnInit
Subscribing here means the component begins listening to data streams as soon as it is ready.
Final Answer:
To start listening to data streams when the component is initialized -> Option A
Quick Check:
Subscribe in ngOnInit = start listening [OK]
Hint: Subscribe in ngOnInit to start data listening early [OK]
Common Mistakes:
Subscribing in ngOnDestroy instead of ngOnInit
Expecting subscription to stop automatically
Confusing subscription with Observable creation
2. Which of the following is the correct way to unsubscribe from an Observable in the ngOnDestroy lifecycle hook?
easy
A. this.subscription.unsubscribe();
B. this.subscription.subscribe();
C. this.subscription.complete();
D. this.subscription.destroy();
Solution
Step 1: Identify the unsubscribe method
Angular subscriptions have an unsubscribe() method to stop listening to Observables.
Step 2: Match method to lifecycle hook
In ngOnDestroy, calling unsubscribe() properly cleans up the subscription.
Final Answer:
this.subscription.unsubscribe(); -> Option A
Quick Check:
Unsubscribe method = unsubscribe() [OK]
Hint: Use unsubscribe() to stop Observable listening [OK]
Common Mistakes:
Using subscribe() instead of unsubscribe() to stop
A. Observable should not be assigned to a property
B. Subscription syntax is incorrect
C. Missing unsubscribe in ngOnDestroy causes memory leak
D. ngOnInit should not contain subscriptions
Solution
Step 1: Check subscription management
The code subscribes to an Observable but never unsubscribes, risking memory leaks.
Step 2: Lifecycle hook missing unsubscribe
Without ngOnDestroy to unsubscribe, the subscription stays active even after component destruction.
Final Answer:
Missing unsubscribe in ngOnDestroy causes memory leak -> Option C
Quick Check:
Always unsubscribe to avoid leaks [OK]
Hint: Always unsubscribe in ngOnDestroy to prevent leaks [OK]
Common Mistakes:
Thinking subscription syntax is wrong
Believing Observables can't be class properties
Assuming ngOnInit forbids subscriptions
5. You want to create an Angular component that listens to a user input Observable and updates a display value. The Observable may emit multiple times quickly. Which approach best manages the subscription lifecycle and avoids memory leaks?
hard
A. Use a global variable to store data and avoid subscribing
B. Subscribe in the constructor and never unsubscribe
C. Subscribe in ngOnInit without unsubscribing, relying on Angular to clean up
D. Subscribe in ngOnInit and unsubscribe in ngOnDestroy using a Subscription object
Solution
Step 1: Proper subscription management
Subscribing in ngOnInit ensures the component starts listening after creation.
Step 2: Unsubscribe in ngOnDestroy to prevent leaks
Unsubscribing cleans up resources when the component is removed, avoiding memory leaks.
Final Answer:
Subscribe in ngOnInit and unsubscribe in ngOnDestroy using a Subscription object -> Option D