【发布时间】:2021-11-30 20:46:08
【问题描述】:
我正在尝试使用 rxjs 捕捉来自 keyup 的最后一个事件,但一直在获取一堆控制台。
这是我的角度指令:
import { Directive, AfterContentChecked, ElementRef, HostListener } from '@angular/core';
import { fromEvent, merge, Observable } from 'rxjs';
import { distinctUntilChanged, debounceTime, last, map } from 'rxjs/operators';
@Directive({
selector: '[appFocuser]'
})
export class FocuserDirective implements AfterContentChecked {
focusableArray = [];
parent = null;
count = 0;
currentFocus: HTMLElement;
constructor(private el: ElementRef) {}
ngAfterContentChecked() {
this.parent = this.el.nativeElement;
this.focusableArray = Array.from(this.parent.querySelectorAll(`[data-focus=true]`));
this.currentFocus = this.focusableArray[0] as HTMLElement;
if (this.currentFocus) {
this.currentFocus.focus();
this.count++;
}
this.eventHandler();
}
eventHandler() {
const events = fromEvent(this.parent, 'keyup')
.pipe(debounceTime(100))
.pipe(distinctUntilChanged());
events.pipe().subscribe(console.log); //each time consoles 7,8
}
//
}
处理这个问题的正确方法是什么?
【问题讨论】:
标签: angular rxjs angular-directive rxjs-pipeable-operators