Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Load Data on Scroll [For Loop]

Tags:

angular

I am showing the data related to the search term. This data shows all the results at once.

What I want to do is show 6 data once and load the remaining on a scroll.

<li *ngFor="let category of categories">
  {{ category.name }}
</li>

How can I show data on scroll?

like image 981
Damon Avatar asked Jun 07 '18 14:06

Damon


People also ask

What is virtual scrolling in angular?

Loading hundreds of elements can be slow in any browser; virtual scrolling enables a performant way to simulate all items being rendered by making the height of the container element the same as the height of total number of elements to be rendered, and then only rendering the items in view.


1 Answers

You could listen to the window:scroll events and load data when the scroll reaches the bottom of the page :

  @HostListener("window:scroll", [])
  onScroll(): void {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
      // Load Your Data Here
    }
  }

Here is a running stackblitz.

like image 124
ibenjelloun Avatar answered Sep 22 '22 10:09

ibenjelloun