Skip to content

Instantly share code, notes, and snippets.

@sankarseran
Created August 9, 2018 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarseran/9101e4a6cb75c8b81d85bcdae3ad34e6 to your computer and use it in GitHub Desktop.
Save sankarseran/9101e4a6cb75c8b81d85bcdae3ad34e6 to your computer and use it in GitHub Desktop.
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