How to track the scroll event of individual cdk-virtual-scroll-viewport

1.8k views Asked by At

I am using cdk-virtual-scroll-viewport to build pagination scroll in my app. I have two instances of cdk-virtual-scroll-viewport, Want to track individual scroll events and make API call for the individual scrolls. How to achieve that?

enter image description here Stackblitz URL https://stackblitz.com/edit/angular-cdk-programatically-scroll-uqgvpu?file=app%2Fcdk-virtual-scroll-fixed-buffer-example.html

2

There are 2 answers

0
Thierry Falvo On

Just use @ViewChild with each component selector.

First in HTML template, declare a template variable for each component, by using hash symbol and name on component tag :

<cdk-virtual-scroll-viewport #cmp1 itemSize="50".../>

Then use @ViewChild decorator in order to query component, and be able to reference it inside code :

@ViewChild('cmp1') cmp1: CdkVirtualScrollViewport;
0
Sasi Kumar M On

Once we register virtual scroll child inventory element

@ViewChild(CdkVirtualScrollViewport, { static: false })

We can subscribe on the elementScrolled observable,

this.cmp1.elementScrolled()
   .subscribe(event => {
    console.log('scrolled');
});