An observable is a way to handle asynchronous data streams. It lets you watch for data changes and react when new data arrives, like listening to a radio station.
Click to reveal answer
beginner
How do you subscribe to an observable in Angular?
You use the subscribe() method on the observable. This tells Angular to start listening and run your code when data comes in.
Click to reveal answer
intermediate
Why should you unsubscribe from observables?
To avoid memory leaks. If you don’t stop listening when the component is gone, your app keeps using memory and can slow down or crash.
Click to reveal answer
intermediate
What is a common way to unsubscribe automatically in Angular?
Use the takeUntil() operator with a Subject that emits when the component is destroyed, or use Angular's async pipe in templates.
Click to reveal answer
intermediate
What are the three callback functions you can pass to subscribe()?
1. Next: runs when new data arrives. 2. Error: runs if something goes wrong. 3. Complete: runs when the observable finishes sending data.
Click to reveal answer
What does the subscribe() method do in Angular?
AStops listening to an observable
BStarts listening to an observable to receive data
CCreates a new observable
DConverts observable to promise
✗ Incorrect
subscribe() tells Angular to listen to the observable and run your code when data arrives.
Which operator helps automatically unsubscribe when a component is destroyed?
AtakeUntil()
Bfilter()
Cmap()
DmergeMap()
✗ Incorrect
takeUntil() listens until a signal (like component destruction) to stop the subscription.
What happens if you forget to unsubscribe from an observable?
ANothing, it’s safe to leave subscriptions open
BThe observable stops automatically
CYour app may leak memory and slow down
DAngular throws an error
✗ Incorrect
Open subscriptions keep using memory, which can slow or crash your app.
Which of these is NOT a callback you can pass to subscribe()?
Acancel
Berror
Ccomplete
Dnext
✗ Incorrect
cancel is not a callback for subscribe(). The three are next, error, and complete.
What is the easiest way to handle subscriptions in Angular templates?
AUse <code>subscribe()</code> in the template
BUse <code>unsubscribe()</code> in the template
CUse <code>setTimeout()</code>
DUse the <code>async</code> pipe
✗ Incorrect
The async pipe automatically subscribes and unsubscribes for you in templates.
Explain how subscribing to an observable works in Angular and why it is important to unsubscribe.
Think about listening to a radio and turning it off when done.
You got /4 concepts.
Describe two ways to manage subscriptions safely in Angular components.
One way uses code in the component, the other uses the template.
You got /3 concepts.
Practice
(1/5)
1. What does subscribing to an observable in Angular allow you to do?
easy
A. React to data or events over time
B. Create new components automatically
C. Change the Angular version
D. Compile TypeScript to JavaScript
Solution
Step 1: Understand the purpose of observables
Observables emit data or events over time, like a stream of values.
Step 2: Role of subscribing
Subscribing lets you listen and react to these emitted values as they arrive.
Final Answer:
React to data or events over time -> Option A
Quick Check:
Subscribing = Reacting to data/events [OK]
Hint: Subscribing means listening to data changes over time [OK]
Common Mistakes:
Thinking subscribing creates components
Confusing subscribing with compilation
Believing subscribing changes Angular version
2. Which is the correct syntax to subscribe to an observable named data$ in Angular?
easy
A. data$.subscribe(value => console.log(value));
B. data$.subscribe = value => console.log(value);
C. subscribe(data$ => console.log(data$));
D. data$.subscribe(console.log());
Solution
Step 1: Identify correct subscribe method usage
The subscribe method is called on the observable with a callback function.
Step 2: Check syntax correctness
data$.subscribe(value => console.log(value)); correctly calls data$.subscribe with an arrow function receiving the emitted value.
Final Answer:
data$.subscribe(value => console.log(value)); -> Option A
The next callback logs each value, then complete logs 'Done'.
Final Answer:
1 2 3 Done -> Option B
Quick Check:
Values then complete message = 1 2 3 Done [OK]
Hint: next logs values, complete logs 'Done' after all [OK]
Common Mistakes:
Assuming complete logs before values
Ignoring complete callback
Expecting error without error emission
4. What is wrong with this subscription code?
this.data$.subscribe(data => {
this.value = data;
});
// No unsubscribe called anywhere
medium
A. It will unsubscribe automatically
B. It will throw a syntax error
C. It will cause a memory leak if component is destroyed
D. It will not receive any data
Solution
Step 1: Identify subscription management
The code subscribes but never unsubscribes, risking resource leaks.
Step 2: Understand Angular component lifecycle
Without unsubscribing, the subscription stays active after component destruction, causing memory leaks.
Final Answer:
It will cause a memory leak if component is destroyed -> Option C
Quick Check:
Missing unsubscribe = memory leak risk [OK]
Hint: Always unsubscribe to avoid memory leaks [OK]
Common Mistakes:
Assuming Angular unsubscribes automatically
Thinking no data will be received
Confusing syntax errors with runtime issues
5. You want to subscribe to two observables obs1$ and obs2$ and combine their latest values whenever either emits. Which RxJS operator should you use before subscribing?
hard
A. filter
B. map
C. catchError
D. combineLatest
Solution
Step 1: Understand the need to combine latest values
You want to react to the latest values from both observables whenever either emits.
Step 2: Identify the correct RxJS operator
combineLatest emits an array of latest values from all observables whenever any emits.