Skip to content

Instantly share code, notes, and snippets.

@kaplan81
Last active February 4, 2020 08:46
Show Gist options
  • Save kaplan81/e48df2dac23a43fc7384258056ac7c3a to your computer and use it in GitHub Desktop.
Save kaplan81/e48df2dac23a43fc7384258056ac7c3a to your computer and use it in GitHub Desktop.
import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
import { interval, Observable, Subject, Subscription } from 'rxjs';
import { takeUntil, tap } from 'rxjs/operators';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './boilerplate.component.html',
})
export class BoilerplateComponent implements OnInit, OnDestroy {
destroyed$ = new Subject<void>();
observable$: Observable<number> = interval(1000);
subscription$$: Subscription;
ngOnInit(): void {
this.subscription$$ = this.observable$
.pipe(tap(console.log), takeUntil(this.destroyed$))
.subscribe();
}
ngOnDestroy(): void {
this.destroyed$.next();
// Any extra actions:
console.log('this.subscription$$.closed in ngOnDestroy:::', this.subscription$$.closed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment