欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular中自定義Debounce Click指令防止重復(fù)點(diǎn)擊

 更新時間:2017年07月26日 09:20:06   作者:semlinker  
本篇文章主要介紹了Angular中自定義Debounce Click指令詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在這篇文章中,我們將介紹使用 Angular Directive API 來創(chuàng)建自定義 debounce click 指令。該指令將處理在指定時間內(nèi)多次點(diǎn)擊事件,這有助于防止重復(fù)的操作。

對于我們的示例,我們希望在產(chǎn)生點(diǎn)擊事件時,實(shí)現(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 指令是沒有模板的組件,我們將使用以下方式應(yīng)用上面的自定義指令:

<button appDebounceClick>Debounced Click</button>

在上面 HTML 代碼中的宿主元素是按鈕,接下來我們要做的第一件事就是監(jiān)聽宿主元素的點(diǎ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 將點(diǎn)擊事件傳遞給我們的 clickEvent() 方法。

在事件處理函數(shù)中,我們可以調(diào)用 event.preventDefault()event.stopPropagation() 方法來阻止瀏覽器的默認(rèn)行為和事件冒泡。

Debounce Events

現(xiàn)在我們可以攔截宿主元素的 click 事件,此時我們還需要有一種方法實(shí)現(xiàn)事件的去抖動處理,然后將它重新發(fā)送回父節(jié)點(diǎn)。這時我們需要借助事件發(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 實(shí)例上的 emit() 方法。

但我們不想立即發(fā)出點(diǎn)擊事件,我們想做去抖動處理。為了實(shí)現(xiàn)這個功能,我們將使用 RxJS 中的 Subject 類。在我們的代碼中,我們創(chuàng)建一個主題來處理我們的點(diǎn)擊事件。在我們的方法中,我們調(diào)用 next() 方法來讓 Subject 對象發(fā)出下一個值。此外我們也使用 RxJS 中 debounceTime 的操作符,這允許我們通過設(shè)置給定的毫秒數(shù)來去抖動點(diǎn)擊事件。

一旦我們設(shè)置好了,我們現(xiàn)在可以在下面的模板中監(jiān)聽我們的自定義去抖動點(diǎn)擊事件。

<button appDebounceClick (debounceClick)="log($event)">
 Debounced Click
</button>

現(xiàn)在,當(dāng)我們點(diǎn)擊我們的按鈕時,它將延遲 500 毫秒。 500毫秒后,我們的自定義輸出屬性將會發(fā)出點(diǎn)擊事件?,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);
  }
}

要取消訂閱,我們需要保存訂閱時返回的訂閱對象。當(dāng) 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 裝飾器允許我們將自定義延遲時間傳遞到我們的組件或指令中。在上面的代碼中,我們可以通過組件的輸入屬性,來指定我們希望去抖動的時間。默認(rèn)情況下,我們將其設(shè)置為 500 毫秒。

<button appDebounceClick (debounceClick)="log($event)" [debounceTime]="300">
 Debounced Click
</button>

參考資源

creating-a-custom-debounce-click-directive-in-angular

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Angular.js中控制器之間的傳值詳解

    Angular.js中控制器之間的傳值詳解

    angular中每個controller(控制器)都會有自己的$scope,通過為這個對象添加屬性賦值,就可以將數(shù)據(jù)傳遞給模板進(jìn)行渲染,每個$scope只會在自己控制器內(nèi)起作用,而有時候需要用到其他控制器中的數(shù)據(jù)就要考慮到控制器之間參數(shù)的傳遞了,下面來看看詳細(xì)的介紹。
    2017-04-04
  • 淺談angular2的http請求返回結(jié)果的subcribe注意事項(xiàng)

    淺談angular2的http請求返回結(jié)果的subcribe注意事項(xiàng)

    下面小編就為大家?guī)硪黄獪\談angular2的http請求返回結(jié)果的subcribe注意事項(xiàng)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • AngularJS 工作原理詳解

    AngularJS 工作原理詳解

    本文主要介紹AngularJS 工作原理,這里整理了相關(guān)資料及示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • angular4應(yīng)用中輸入的最小值和最大值的方法

    angular4應(yīng)用中輸入的最小值和最大值的方法

    這篇文章主要介紹了angular4應(yīng)用中輸入的最小值和最大值的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 淺析AngularJs HTTP響應(yīng)攔截器

    淺析AngularJs HTTP響應(yīng)攔截器

    這篇文章主要介紹了淺析AngularJs HTTP響應(yīng)攔截器的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 使用AngularJS 應(yīng)用訪問 Android 手機(jī)的圖片庫

    使用AngularJS 應(yīng)用訪問 Android 手機(jī)的圖片庫

    這篇文章主要介紹了使用AngularJS 應(yīng)用訪問 Android 手機(jī)的圖片庫的相關(guān)資料,需要的朋友可以參考下
    2015-03-03
  • Angularjs實(shí)現(xiàn)下拉框聯(lián)動的示例代碼

    Angularjs實(shí)現(xiàn)下拉框聯(lián)動的示例代碼

    本篇文章主要介紹了Angularjs下拉框聯(lián)動的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 簡述Angular 5 快速入門

    簡述Angular 5 快速入門

    這篇文章主要介紹了簡述Angular 5 快速入門,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Angular?項(xiàng)目實(shí)現(xiàn)國際化的方法

    Angular?項(xiàng)目實(shí)現(xiàn)國際化的方法

    本篇文章主要介紹了Angular?項(xiàng)目實(shí)現(xiàn)國際化的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧<BR>
    2018-01-01
  • angularJS實(shí)現(xiàn)表格部分列展開縮起示例代碼

    angularJS實(shí)現(xiàn)表格部分列展開縮起示例代碼

    這篇文章主要介紹了angularJS實(shí)現(xiàn)表格部分列展開縮起示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09

最新評論