当前位置:   article > 正文

Angular防抖指令_angular5 请求防抖

angular5 请求防抖

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

防抖公共文件

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();
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

点击防抖文件

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);
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

输入防抖文件

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);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

导出指令

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 {
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

实际使用

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

<input (debounceClick)="enterSearch()" appDebounceClick [(ngModel)]="searchText" />
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号