Last active
March 10, 2022 00:19
-
-
Save ZhenDeng/b947127c583546b1a36a173c3ff245b6 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 { 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); | |
} | |
} |
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
<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