Skip to content

Instantly share code, notes, and snippets.

@ZhenDeng
Last active March 10, 2022 00:19
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 ZhenDeng/b947127c583546b1a36a173c3ff245b6 to your computer and use it in GitHub Desktop.
Save ZhenDeng/b947127c583546b1a36a173c3ff245b6 to your computer and use it in GitHub Desktop.
import { Directive, OnInit, HostListener, Output, EventEmitter, Input } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
@Directive({
selector: '[appDebounceClick]'
})
export class DebounceClickDirective implements OnInit {
@Input() debounceTime = 500;
@Output() debounceClick = new EventEmitter();
private clicks = new Subject();
private subscription: Subscription;
constructor() {
}
ngOnInit() {
this.subscription = this.clicks
.pipe(debounceTime(this.debounceTime))
.subscribe(e => this.debounceClick.emit(e));
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
@HostListener('click', ['$event'])
clickEvent(event) {
event.preventDefault();
event.stopPropagation();
console.log('Click from Host Element!');
this.clicks.next(event);
}
}
<button appDebounceClick type="button" (debounceClick)= "login()">Login</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment