Subscribing to observables lets your app listen for data changes or events over time. It helps you react when new data arrives.
Subscribing to observables in Angular
observable$.subscribe({
next: value => { /* handle new value */ },
error: err => { /* handle error */ },
complete: () => { /* handle completion */ }
});The subscribe method takes an object with up to three callbacks: next, error, and complete.
You can also pass just a function to handle next values if you don't need error or completion handling.
next callback to log each new value.myObservable.subscribe(value => {
console.log('Got value:', value);
});myObservable.subscribe({
next: value => console.log(value),
error: err => console.error('Error:', err),
complete: () => console.log('Done')
});This Angular component shows seconds passed by subscribing to an observable that emits a number every second. The displayed number updates automatically.
import { Component } from '@angular/core'; import { interval } from 'rxjs'; @Component({ selector: 'app-timer', template: `<p>Seconds passed: {{seconds}}</p>` }) export class TimerComponent { seconds = 0; constructor() { const timer$ = interval(1000); // emits 0,1,2,... every second timer$.subscribe({ next: val => this.seconds = val + 1, error: err => console.error('Timer error:', err), complete: () => console.log('Timer completed') }); } }
Always unsubscribe from observables in Angular components to avoid memory leaks, especially for long-lived observables.
Use Angular's async pipe in templates when possible to handle subscription automatically.
Subscribing lets you react to data or events over time.
Use subscribe with callbacks for next, error, and complete.
Remember to manage subscriptions to keep your app efficient.
Practice
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 AQuick Check:
Subscribing = Reacting to data/events [OK]
- Thinking subscribing creates components
- Confusing subscribing with compilation
- Believing subscribing changes Angular version
data$ in Angular?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 callsdata$.subscribewith an arrow function receiving the emitted value.Final Answer:
data$.subscribe(value => console.log(value)); -> Option AQuick Check:
Correct subscribe syntax = data$.subscribe(value => console.log(value)); [OK]
- Assigning subscribe instead of calling it
- Calling subscribe as a standalone function
- Calling console.log() prematurely
const obs$ = of(1, 2, 3);
obs$.subscribe({
next: x => console.log(x),
error: err => console.error(err),
complete: () => console.log('Done')
});What will be the console output?
Solution
Step 1: Understand observable emissions
The observable emits values 1, 2, 3 in sequence.Step 2: Analyze subscription callbacks
Thenextcallback logs each value, thencompletelogs 'Done'.Final Answer:
1 2 3 Done -> Option BQuick Check:
Values then complete message = 1 2 3 Done [OK]
- Assuming complete logs before values
- Ignoring complete callback
- Expecting error without error emission
this.data$.subscribe(data => {
this.value = data;
});
// No unsubscribe called anywhereSolution
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 CQuick Check:
Missing unsubscribe = memory leak risk [OK]
- Assuming Angular unsubscribes automatically
- Thinking no data will be received
- Confusing syntax errors with runtime issues
obs1$ and obs2$ and combine their latest values whenever either emits. Which RxJS operator should you use before subscribing?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
combineLatestemits an array of latest values from all observables whenever any emits.Final Answer:
combineLatest -> Option DQuick Check:
Combine latest values = combineLatest operator [OK]
- Using map which transforms but doesn't combine
- Using filter which only filters emissions
- Using catchError which handles errors, not combining
Ready to prove it?
Pass the test and this pattern moves unknown
