State Management - Properly Unsubscribing

Angular: The Full Gamut Edition

Charlie Greenman
April 19, 2021
2 min read

In Angular, when using ngrx when trying to pull in data, using the async pipe is the preferred approach. It will handle both the subscribe and the unsubscribe from the observable pipe for you.

<div>{{ observableStream$ | async }}</div>

What to do When Async Pipe is Not an Option

There are times When the process of subscribing and unsubscribing must be managed manually. In particular in situation where there is data manipilation that must happen within the component. In this case the recommended approach is to create an observable that emits when the component is destroyed, and use the rxjs takeUntil operator to handle the act of unsubscribing for you.

Using takUntil Example

import { Subject, Observable, pipe } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { MyService } from './my-service';

  selector: 'my-component',
  template: `
      Count: {{ count }}
export class MyComponent {
  private destroy$ = new Subject();
  count: number;

  constructor(private myService: MyService) { }

  ngOnInit() {
      .subscribe(count => this.count = count);

  ngOnDestroy() {

takeUntil in Depth

Let's go back in depth to takeUntil to see what we are doing:

private destroy$ = new Subject();

We are create a subject which, of course, acts as both an observer and an observable. It allows us to call next and complete. We can then pass the value of true to next:

ngOnDestroy() {

Which is called when the component is destroyed. We then run takeUntil, which will unsubscribe as soon as it is passed a true value.


Subscribe to the Razroo Angular Newsletter!

Razroo takes pride in it's Angular newsletter, and we really pour heart and soul into it. Pass along your e-mail to recieve it in the mail. Our commitment, is to keep you up to date with the latest in Angular, so you don't have to.

More articles similar to this