Complete the code to subscribe to the observable in the component.
this.dataService.getData().[1](data => {
this.items = data;
});The subscribe method is used to listen to observable data and react when data arrives.
Complete the code to unsubscribe from the observable when the component is destroyed.
ngOnDestroy() {
this.subscription?.[1]();
}The unsubscribe method stops the observable subscription to prevent memory leaks.
Fix the error in the code to properly assign the subscription.
this.[1] = this.dataService.getData().subscribe(data => {
this.items = data;
});The subscription object should be stored in a variable named subscription to manage it later.
Fill both blanks to create a safe subscription that unsubscribes automatically.
import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; private destroy$ = new [1](); this.dataService.getData() .pipe([2](this.destroy$)) .subscribe(data => { this.items = data; });
Using a Subject as a notifier and the takeUntil operator ensures the subscription ends when destroy$ emits.
Fill all three blanks to complete the component lifecycle with observable cleanup.
import { Component, OnDestroy } from '@angular/core'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; @Component({ selector: 'app-example', templateUrl: './example.component.html' }) export class ExampleComponent implements OnDestroy { private [1] = new Subject<void>(); constructor(private dataService: DataService) { this.dataService.getData() .pipe([2](this.[1])) .subscribe(data => { console.log(data); }); } ngOnDestroy() { this.[1].[3](); } }
The destroy$ Subject emits a signal with next() in ngOnDestroy, and takeUntil uses it to complete the subscription safely.