Last active
January 16, 2025 12:32
-
-
Save svetlio/8997fe4591c38095f57e17a759b31720 to your computer and use it in GitHub Desktop.
Angular 5 directive - Prevent double click for html button (no-dbl-click.directive.ts), and material mat-button (no-dbl-click-mat.directive.ts)
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 { Directive, HostListener } from '@angular/core'; | |
@Directive({ | |
selector: '[appNoDblClickMat]' | |
}) | |
export class NoDblClickDirectiveMat { | |
constructor() { } | |
@HostListener('click', ['$event']) | |
clickEvent(event) { | |
event.srcElement.parentElement.setAttribute('disabled', true); | |
setTimeout(function(){ | |
event.srcElement.parentElement.removeAttribute('disabled'); | |
}, 1000); | |
} | |
} |
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 { Directive, HostListener } from '@angular/core'; | |
@Directive({ | |
selector: '[appNoDblClick]' | |
}) | |
export class NoDblClickDirective { | |
constructor() { } | |
@HostListener('click', ['$event']) | |
clickEvent(event) { | |
event.srcElement.setAttribute('disabled', true); | |
setTimeout(function(){ | |
event.srcElement.removeAttribute('disabled'); | |
}, 500); | |
} | |
} |
Thanks !
To make it work for buttons and material buttons, you can use event.currentTarget
.
The current target will refer directly to the <button>
as you attached the event handler there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://coryrylan.com/blog/creating-a-custom-debounce-click-directive-in-angular