1
Hello,
I have a directive to lock the double click buttons on my system... However, when clicking a button that is "calling" the directive, it does not do its function (e.g., if the button is Submit, Submit does not happen).
I’d like to use it to several buttons with different functions.
Directive
import { Directive, HostListener } from '@angular/core';
@Directive({
selector: '[blockClicks]'
})
export class NoDblClickMatDirective {
constructor() { }
@HostListener('click', ['$event'])
clickEvent(event) {
const button = (event.srcElement.disabled === undefined) ? event.srcElement.parentElement : event.srcElement;
button.setAttribute('disabled', true);
setTimeout(function () {
button.removeAttribute('disabled');
}, 2000);
}
}
Module Directive
import { NgModule } from "@angular/core";
import { NoDblClickMatDirective } from "./nodblclickmat.directive";
@NgModule({
imports: [],
declarations: [NoDblClickMatDirective],
exports: [NoDblClickMatDirective]
})
export class NoDblClickMatDirectiveModule { }
Html snippet
<button class="btn btn-info" blockClicks>Entrar</button>
I assume you tested that on Google Chrome. I’ve never done background research, but there are browsers that don’t submit the form if you set the button as
disabled
. If I’m not mistaken it happened a while ago and in Firefox worked normally, Chrome blocked.– Woss
Hmmm, @Andersoncarloswoss, I will test on other browsers... If that’s the case, I would solve how?
– LeoHenrique
@Andersoncarloswoss You’re right, in Firefox it worked...
– LeoHenrique
I hear this a lot :D Usually you can get around it by disabling the button in the event
submit
form, but in this case maybe you can check if it’s a button likesubmit
and manually force commit.– Woss
You think disable setar to readonly can solve?
– LeoHenrique
A button can’t be
readonly
.– Woss
Oops, I gaffed kkk
– LeoHenrique
Can you help me a little more on how I would do the event? I had done in the function of Submit, but as I will use in several buttons, I wanted to do a thing
– LeoHenrique