Angular中自定義Debounce Click指令防止重復點擊
在這篇文章中,我們將介紹使用 Angular Directive API 來創(chuàng)建自定義 debounce click 指令。該指令將處理在指定時間內(nèi)多次點擊事件,這有助于防止重復的操作。
對于我們的示例,我們希望在產(chǎn)生點擊事件時,實現(xiàn)去抖動處理。接下來我們將介紹 Directive API,HostListener API 和 RxJS 中 debounceTime 操作符的相關(guān)知識。首先,我們需要創(chuàng)建 DebounceClickDirective 指令并將其注冊到我們的 app.module.ts 文件中:
import { Directive, OnInit } from '@angular/core';
@Directive({
selector: '[appDebounceClick]'
})
export class DebounceClickDirective implements OnInit {
constructor() { }
ngOnInit() { }
}
@NgModule({
imports: [BrowserModule],
declarations: [
AppComponent,
DebounceClickDirective
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Angular 指令是沒有模板的組件,我們將使用以下方式應用上面的自定義指令:
<button appDebounceClick>Debounced Click</button>
在上面 HTML 代碼中的宿主元素是按鈕,接下來我們要做的第一件事就是監(jiān)聽宿主元素的點擊事件,因此我們可以將以下代碼添加到我們的自定義指令中。
import { Directive, HostListener, OnInit } from '@angular/core';
@Directive({
selector: '[appDebounceClick]'
})
export class DebounceClickDirective implements OnInit {
constructor() { }
ngOnInit() { }
@HostListener('click', ['$event'])
clickEvent(event: MouseEvent) {
event.preventDefault();
event.stopPropagation();
console.log('Click from Host Element!');
}
}
在上面的例子中,我們使用了 Angular @HostListener 裝飾器,該裝飾器允許你輕松地監(jiān)聽宿主元素上的事件。在我們的示例中,第一個參數(shù)是事件名。第二個參數(shù) $event,這用于告訴 Angular 將點擊事件傳遞給我們的 clickEvent() 方法。
在事件處理函數(shù)中,我們可以調(diào)用 event.preventDefault() 和 event.stopPropagation() 方法來阻止瀏覽器的默認行為和事件冒泡。
Debounce Events
現(xiàn)在我們可以攔截宿主元素的 click 事件,此時我們還需要有一種方法實現(xiàn)事件的去抖動處理,然后將它重新發(fā)送回父節(jié)點。這時我們需要借助事件發(fā)射器和 RxJS 中的 debounce 操作符。
import { Directive, EventEmitter, HostListener, OnInit, Output } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/debounceTime';
@Directive({
selector: '[appDebounceClick]'
})
export class DebounceClickDirective implements OnInit {
@Output() debounceClick = new EventEmitter();
private clicks = new Subject<any>();
constructor() { }
ngOnInit() {
this.clicks
.debounceTime(500)
.subscribe(e => this.debounceClick.emit(e));
}
@HostListener('click', ['$event'])
clickEvent(event: MouseEvent) {
event.preventDefault();
event.stopPropagation();
this.clicks.next(event);
}
}
在上面的代碼中,我們使用 Angular @Output 屬性裝飾器和 EventEmitter 類,它們允許我們在指令上創(chuàng)建自定義事件。要發(fā)出事件,我們需要調(diào)用 EventEmitter 實例上的 emit() 方法。
但我們不想立即發(fā)出點擊事件,我們想做去抖動處理。為了實現(xiàn)這個功能,我們將使用 RxJS 中的 Subject 類。在我們的代碼中,我們創(chuàng)建一個主題來處理我們的點擊事件。在我們的方法中,我們調(diào)用 next() 方法來讓 Subject 對象發(fā)出下一個值。此外我們也使用 RxJS 中 debounceTime 的操作符,這允許我們通過設置給定的毫秒數(shù)來去抖動點擊事件。
一旦我們設置好了,我們現(xiàn)在可以在下面的模板中監(jiān)聽我們的自定義去抖動點擊事件。
<button appDebounceClick (debounceClick)="log($event)"> Debounced Click </button>
現(xiàn)在,當我們點擊我們的按鈕時,它將延遲 500 毫秒。 500毫秒后,我們的自定義輸出屬性將會發(fā)出點擊事件?,F(xiàn)在我們有了基本的功能,我們需要做一些清理工作,并增加一些其它的功能。
Unsubscribe
對于 RxJS 中 Observables 和 Subject 對象,一旦我們不再使用它們,我們必須取消訂閱事件。如果我們沒有執(zhí)行取消訂閱操作,有可能會出現(xiàn)內(nèi)存泄漏。
import { Directive, EventEmitter, HostListener, OnInit, Output, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Subscription } from "rxjs/Subscription";
import 'rxjs/add/operator/debounceTime';
@Directive({
selector: '[appDebounceClick]'
})
export class DebounceClickDirective implements OnInit, OnDestroy {
@Output() debounceClick = new EventEmitter();
private clicks = new Subject<any>();
private subscription: Subscription;
constructor() { }
ngOnInit() {
this.subscription = this.clicks
.debounceTime(500)
.subscribe(e => this.debounceClick.emit(e));
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
@HostListener('click', ['$event'])
clickEvent(event: MouseEvent) {
event.preventDefault();
event.stopPropagation();
this.clicks.next(event);
}
}
要取消訂閱,我們需要保存訂閱時返回的訂閱對象。當 Angular 銷毀組件時,它將調(diào)用 OnDestroy 生命周期鉤子,因此我們可以在這個鉤子中,執(zhí)行取消訂閱操作。
Custom Inputs
我們指令的功能已基本齊全,它可以正常處理事件。接下來,我們將添加一些更多的邏輯,以便我們可以自定義去抖動時間。為此,我們將使用 @Input 裝飾器。
import { Directive, EventEmitter, HostListener, OnInit, Output, OnDestroy, Input } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Subscription } from "rxjs/Subscription";
import 'rxjs/add/operator/debounceTime';
@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
.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);
}
}
@Input 裝飾器允許我們將自定義延遲時間傳遞到我們的組件或指令中。在上面的代碼中,我們可以通過組件的輸入屬性,來指定我們希望去抖動的時間。默認情況下,我們將其設置為 500 毫秒。
<button appDebounceClick (debounceClick)="log($event)" [debounceTime]="300"> Debounced Click </button>
參考資源
creating-a-custom-debounce-click-directive-in-angular
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談angular2的http請求返回結(jié)果的subcribe注意事項
下面小編就為大家?guī)硪黄獪\談angular2的http請求返回結(jié)果的subcribe注意事項。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
使用AngularJS 應用訪問 Android 手機的圖片庫
這篇文章主要介紹了使用AngularJS 應用訪問 Android 手機的圖片庫的相關(guān)資料,需要的朋友可以參考下2015-03-03
Angularjs實現(xiàn)下拉框聯(lián)動的示例代碼
本篇文章主要介紹了Angularjs下拉框聯(lián)動的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
angularJS實現(xiàn)表格部分列展開縮起示例代碼
這篇文章主要介紹了angularJS實現(xiàn)表格部分列展開縮起示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

