简体   繁体   English

角度4:无限滚动默认行为不起作用

[英]Angular 4: Infinite scroll default behavior not working

I am trying to implement infinite scroll in my Angular 4 application. 我正在尝试在Angular 4应用程序中实现无限滚动。 I have followed all the directions from https://www.npmjs.com/package/ngx-infinite-scroll 我遵循了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 为元素设置一个显式的css“ height”值

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. 相反,如果我为div元素设置高度并设置[scrollWindow] =“ false”,则在这种情况下它可以工作。 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. 谢谢。

You can achieve infinite scroll functionality without installing extra packages. 您无需安装额外的程序包就可以实现无限滚动功能。 The below example works for me. 以下示例对我有用。

app.component.ts 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 和你的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>

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

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