Skip to content

Instantly share code, notes, and snippets.

@alxhub
Created December 20, 2017 16:54
Show Gist options
  • Save alxhub/b2b12c1c8fc258f993a828764555120d to your computer and use it in GitHub Desktop.
Save alxhub/b2b12c1c8fc258f993a828764555120d to your computer and use it in GitHub Desktop.
import {Component, OnDestroy} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Subject} from 'rxjs/Subject';
import {takeUntil} from 'rxjs/operators/takeUntil';
@Component({
selector: '...', template: '...'
})
export class MyCmp implements OnDestroy {
onDestroy$ = new Subject<void>();
data: any;
constructor(http: HttpClient) {
this
.http
.get('/url')
.pipe(
takeUntil(this.onDestroy$)
)
.subscribe(res => this.data = res);
}
ngOnDestroy(): void {
this.onDestroy$.next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment