Created
August 9, 2018 12:23
-
-
Save sankarseran/9101e4a6cb75c8b81d85bcdae3ad34e6 to your computer and use it in GitHub Desktop.
This file contains 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 { EventEmitter, ElementRef, OnInit, Directive, Input, Output } from '@angular/core'; | |
import { Observable } from 'rxjs'; | |
import { NgModel } from '@angular/forms'; | |
@Directive({ selector: '[debounce]' }) | |
export class DebounceDirective implements OnInit { | |
@Input() delay: number = 700; | |
@Output() keyUpFunc: EventEmitter<any> = new EventEmitter(); | |
constructor(private elementRef: ElementRef, private model: NgModel) { | |
} | |
ngOnInit(): void { | |
const eventStream = Observable.fromEvent(this.elementRef.nativeElement, 'keyup') | |
.map(() => this.model.value) | |
.debounceTime(this.delay); | |
eventStream.subscribe(input => this.keyUpFunc.emit(input)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment