Angular防抖指令

之前做了搜索按钮的防抖指令,后面需要在搜索条件输入框加回车搜索功能,需要扩展优化,顺便记录一下!

防抖公共文件

import { Directive, Input, OnDestroy, OnInit } from '@angular/core';
import { Subject } from 'rxjs';
import {debounceTime, takeUntil} from 'rxjs/operators';

@Directive({})
export abstract class AbstractDebounceDirective implements OnInit, OnDestroy {
  @Input() public debounceTime = 300;

  protected emitEvent$ = new Subject<Event>();

  protected destroyed$ = new Subject<void>();

  public ngOnInit(): void {
    this.emitEvent$
      .pipe(debounceTime(this.debounceTime), takeUntil(this.destroyed$))
      .subscribe((value) => this.emitValue(value));
  }

  public abstract emitValue(event: Event): void;

  public ngOnDestroy(): void {
    this.destroyed$.next();
    this.destroyed$.complete();
  }
}

点击防抖文件

import { Directive, EventEmitter, HostListener, Output } from '@angular/core';
import { AbstractDebounceDirective } from './abstract-debounce.directive';

@Directive({
  selector: '[appDebounceClick]',
})
export class DebounceClickDirective extends AbstractDebounceDirective {
  @Output() public debounceClick = new EventEmitter<Event>();

  constructor() {
    super();
  }

  @HostListener('click', ['$event'])
  public onClick(event: MouseEvent): void {
    event.preventDefault();
    this.emitEvent$.next(event);
  }

  public emitValue(event: Event): void {
    this.debounceClick.emit(event);
  }
}

输入防抖文件

import { Directive, EventEmitter, HostListener, Output } from '@angular/core';
import { AbstractDebounceDirective } from './abstract-debounce.directive';

@Directive({
  selector: '[appDebounceInput]',
})
export class DebounceInputDirective extends AbstractDebounceDirective {
  @Output() public debounceInput = new EventEmitter<Event>();

  constructor() {
    super();
  }

  @HostListener('keyup', ['$event'])
  public onClick(event: KeyboardEvent): void {
  	// 这里的判断是用于回车搜索的输入框,如果需要输入实时搜索可将此判断去除
    if (event.key === 'Enter') {
      event.preventDefault();
      this.emitEvent$.next(event);
    }
  }

  public emitValue(event: KeyboardEvent): void {
    this.debounceInput.emit(event);
  }
}

导出指令

import { NgModule } from '@angular/core';
import { DebounceClickDirective } from './debounce.directive';
import { DebounceInputDirective } from './debounceInput.directive';


@NgModule({
  declarations: [
    DebounceClickDirective,
    DebounceInputDirective
  ],
  exports: [
    DebounceClickDirective,
    DebounceInputDirective
  ],
})
export class SharedDirectivesModule {
}

实际使用

<span (debounceClick)="export()" appDebounceClick [debounceTime]="300">导出模版</span>

<input (debounceClick)="enterSearch()" appDebounceClick [(ngModel)]="searchText" />
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值