How to prevent double click in Angular?

前端 未结 5 1808
南旧
南旧 2020-12-24 02:49

I have a component with click.


When I click this element, openModa

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 03:35

    I propose a simpler approach for buttons:

    import {Directive, ElementRef, HostListener} from '@angular/core';
    
    const DISABLE_TIME = 300;
    
    @Directive({
        selector: 'button[n-submit]'
    })
    export class DisableButtonOnSubmitDirective {
        constructor(private elementRef: ElementRef) { }
        @HostListener('click', ['$event'])
        clickEvent() {
            this.elementRef.nativeElement.setAttribute('disabled', 'true');
            setTimeout(() => this.elementRef.nativeElement.removeAttribute('disabled'), DISABLE_TIME);
        }
    }
    

    Example usage:

    
    

提交回复
热议问题