繁体   English   中英

如何在angular2中重置RxJS

[英]How to reset RxJS distinct in angular2

我需要实现无限滚动,因为后端响应仅限于100个项目。 因此,在首页尝试中,我从后端收到100项。 在每次滚动到页面末尾时,我都需要另外100个项目的调用终结点。

在我的子组件( BpHistoryListInfiniteComponent )中,我设置了data-load属性以跟踪100.元素何时出现。 然后在数据加载属性的Subject( pageByScroll$ )值中设置IntersectionObserver的情况下。 我的pageByScroll$需要以0开头(用于第一次尝试页面),并且我使用distinct()因为我需要distinct已加载项,然后将该值发送给父组件。

但是在父组件中使用了一个过滤器之后,我需要将index值重置为0并将其发送并将过滤器值发送到后端(并且仅接收100个项目),然后如果用户滚动到底部,我需要再次将index从0增加但distinct()不允许我这样做。

我需要以某种方式重置不同的值。

// html子组件的一部分

<div #infiniteScrollMadness class="column">
  <div *ngFor="let item of history; let i = index" class="list" [attr.data-load]="(i + 1) % 100 == 0 ? (i + 1) : null">
    <span>{{ item.id }}</span>
    <span>{{ item.name }}</span>
  </div>
</div>

.ts子组件的一部分

export class BpHistoryListInfiniteComponent implements AfterViewInit {
    public pageByScroll$ = new Subject<number>();

    @ViewChild("infiniteScrollMadness")
    private scrollHost: ElementRef;

    @Input()
    history: HistoryModel;

    @Input()
    highlight: string;

    @Output()
    index = new EventEmitter<number>();

    ngAfterViewInit() {
        const intersectionObserver = new IntersectionObserver(
            entries => {
                entries
                    .filter(element => element.isIntersecting)
                    .forEach(element => {
                        this.pageByScroll$.next(
                            element.target.attributes["data-load"].value
                        );
                    });
            },
            {
                threshold: 0.1
            }
        );
        const mutationObserver = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                if (!mutation.addedNodes || mutation.type !== "childList") {
                    return;
                }
                const nodes = Array.prototype.slice.call(mutation.addedNodes, 0);

                nodes.filter(node => node.nodeType === Node.ELEMENT_NODE)
                    .forEach((element: Element) => {
                        if (element.attributes["data-load"]) {
                            this.zone.runOutsideAngular(() => {
                                intersectionObserver.observe(element);
                            });
                        }
                    });
            });
        });

        mutationObserver.observe(this.scrollHost.nativeElement, {
            childList: true
        });

        this.pageByScroll$.pipe(
            startWith(0),
            distinct()
        ).subscribe(index => this.index.emit(index));
    }

    constructor(private zone: NgZone) {}
}

pageByScroll$流:
-首页尝试=>值:0
-滚动到底部(+100)=>值:100
-滚动到底部(+100)=>值:200
-滚动到底部(+100)=>值:300
-使用过滤器之一=>值:0
-滚动到底部(+100)=>值:100
-滚动到底部(+100)=>值:200

你可以这样处理

this.resets$ = new Subject();
this.resets$.pipe(
  startWith(null),
  switchMap(() => this.pageByScroll$.pipe(
    startWith(0),
    distinct()
  ))
).subscribe(this.index);

这个将“作用域” distinct运算符切换到switchMap ,您可以通过this.resets$.next(null)手动将其重置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM