0
0
Angularframework~5 mins

Subscribing to observables in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an observable in Angular?
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.<br>2. Error: runs if something goes wrong.<br>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
Which operator helps automatically unsubscribe when a component is destroyed?
AtakeUntil()
Bfilter()
Cmap()
DmergeMap()
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
Which of these is NOT a callback you can pass to subscribe()?
Acancel
Berror
Ccomplete
Dnext
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
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.