1

I am trying to implement infinite scroll in my Angular 4 application. I have followed all the directions from https://www.npmjs.com/package/ngx-infinite-scroll

In the document it says:

By default, the directive listens to the window scroll event and invoked the callback. To trigger the callback when the actual element is scrolled, these settings should be configured:

  • [scrollWindow]="false"
  • set an explict css "height" value to the element

But the default window scroll event is not triggered in my case. Instead if i set a height for a div element and set [scrollWindow] = "false", then it works in that case. I do not know what am I missing.

Example in given document imports

{ platformBrowserDynamic } from '@angular/platform-browser-dynamic';

but i have not imported that in my module. Is that causing the issue. I think it's not the cause.

Any help will be appreciated. Thanks.

Prabesh
  • 327
  • 1
  • 4
  • 17

1 Answers1

3

You can achieve infinite scroll functionality without installing extra packages. The below example works for me.

app.component.ts

export class AppComponent
{
    stones: Array<any>
    margin: number = 10;

    constructor() {

        this.stones = new Array<Object>();
        this.populate(this.margin);
    }

    onScroll(event: any) {

        if (((window.innerHeight + window.scrollY + 1) >= document.body.offsetHeight) || ((window.innerHeight + document.documentElement.scrollTop) >= document.body.offsetHeight)) {
            this.populate(this.margin);
        }
    }

    populate(margin: number): void {

        for (let i = 0; i < margin; i++) {
            this.stones.push(new Object());
        }
    }
}

and your app.component.html

<div (window:scroll)="onScroll($event)">
    <div *ngFor="let item of stones">
        <div style="width:100px; height:60px; background-color:green;margin:20px"></div>
    </div>
</div>
armache
  • 546
  • 3
  • 19
  • I give you plus event that your solution do not work, but you gave me good idea instead. My solution was `if ((document.body.scrollHeight - window.innerHeight - window.scrollY) < 50) {...}` – x-magix Jul 15 '20 at 22:09