Skip to content

Instantly share code, notes, and snippets.

@svetlio
Last active April 7, 2023 09:48
Show Gist options
  • Save svetlio/8997fe4591c38095f57e17a759b31720 to your computer and use it in GitHub Desktop.
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)
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);
}
}
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);
}
}
@nbrdx-zenika
Copy link

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