赞
踩
import {Directive, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output} from '@angular/core'; import {Subject, Subscription} from 'rxjs'; import {debounceTime} from 'rxjs/operators'; @Directive({ selector: '[appDebounceClick]' }) export class DebounceClickDirective implements OnInit, OnDestroy { @Input() debounceTime = 500; @Output() debounceClick = new EventEmitter(); private clicks = new Subject<any>(); 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: MouseEvent) { event.preventDefault(); event.stopPropagation(); this.clicks.next(event); } }
<button
type="submit"
appDebounceClick
(debounceClick)="confirmAddUser(registerData)"
[debounceTime]="300"
>
确认
</button>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。