Created
May 30, 2018 12:54
-
-
Save yjaaidi/9baf3f077a24ac07e4d0569ae46db3cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnDestroy, OnInit } from '@angular/core'; | |
import { Subject, interval } from 'rxjs'; | |
import { takeUntil } from 'rxjs/operators'; | |
@Component({ | |
template: ` | |
<div>{{ count }}</div> | |
<button (click)="startCounting()">Start Counting</button> | |
` | |
}) | |
export class CounterComponent implements OnInit, OnDestroy { | |
count: number; | |
private _destroyed$ = new Subject(); | |
ngOnInit() { | |
this.startCounting(); | |
} | |
ngOnDestroy() { | |
this._destroyed$.next(); | |
this._destroyed$.complete(); | |
} | |
startCounting() { | |
interval(1000) | |
.pipe(takeUntil(this._destroyed$)) | |
.subscribe(count => this.count = count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment