Mur*_*sli 3 rxjs angular pairwise
我正在尝试为垂直和水平滚动方向创建两个可观察的滚动事件。
我尝试使用pairwise()和bufferCount(2,1)运算符从水平滚动事件中过滤垂直滚动事件,但问题是获取prev.scrollTop和 的重复值curr.scrollTop
import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import { fromEvent } from 'rxjs';
import { pairwise, tap, filter } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
@ViewChild('scrollable', {static: false}) scrollable: ElementRef;
ngAfterViewInit() {
fromEvent(this.scrollable.nativeElement, 'scroll').pipe(
pairwise(),
tap(([prev, curr]) => console.log(prev.target.scrollTop, curr.target.scrollTop)),
filter(([prev, curr]) => prev.target.scrollTop !== curr.target.scrollTop),
tap((e) => console.log(e)) // <= Never reached
).subscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
这是因为您成对使用了 nativeElement,它始终引用同一个对象。所以基本上你必须得到pluck你想要的原始价值。
fromEvent(this.scrollable.nativeElement, 'scroll').pipe(
pluck('target','scrollTop'),
pairwise(),
tap(([prev, curr]) => console.log(prev,curr)),
filter(([prev, curr]) => prev!== curr),
tap((e) => console.log(e)) // <= Never reached
).subscribe();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6409 次 |
| 最近记录: |