我是角度4的新手。我想在角度4中实现滚动分页。最初我要显示20条记录。 向下滚动后我想显示下一个20.我会做同样的事情直到列表结束。
我尝试使用“angular2-infinite-scroll”来实现它。但我无法初步显示前20条记录以及滚动数据。
组件文件:
import { Component, OnInit } from '@angular/core';
import { InfiniteScrollModule } from 'angular2-infinite-scroll';
@Component({
selector: 'app-scroll',
templateUrl: `./scroll.component.html`,
styleUrls: ['./scroll.component.css']
})
export class ScrollComponent implements OnInit {
let item = [{
"Name": "XYz Compnay"
},
{
"Name": "XYz Company1"
}, {
"Name": "XYz Company2"
}, {
"Name": "XYz Company3"
}, {
"Name": "XYz Company4"
}, {
"Name": "XYz Company5"
}];
constructor() {}
ngOnInit() {}
onScroll () {
alert('scrolled!!');
}
}
HTML文件:
<div
infinite-scroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="10"
(scrolled)="onScroll()"
>
<p *ngFor="let items of item">
{{items.Name}}
</p>
</div>
如果有人有相关信息,请分享。
答案 0 :(得分:8)
由于新功能和兼容性,我建议对angular2-infinite-scroll
使用ngx-infinite-scroll
与AOT。
如果您没有使用整个窗口并使用scrollWindow
,首先需要指定overflow: scroll
属性
用于模拟可滚动div的属性。同样在ScrollComponent
课程中,您需要item
作为课程的属性
而不是变量,因此您应该使用public item
而不是let item
。
如果已知列表大小,则可以利用infiniteScrollDisabled
来提高性能。
此外,将多个内容命名为item
并将单个内容称为item
在语法上不正确。我要去
将item
更改为items
(您可以在模板html中的ngFor循环中看到)
@Component({
selector: 'my-app',
styles: [`
.search-results {
height: 100px;
overflow: scroll;
}
`],
template: `
<div class="search-results"
infiniteScroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="50"
(scrolled)="onScroll()"
[infiniteScrollDisabled]="isFullListDisplayed"
[scrollWindow]="false">
<p *ngFor="let item of itemsToShow; let i = index">
{{i + ' ' + item.Name}}
</p>
</div>
`
})
export class AppComponent {
private noOfItemsToShowInitially: number = 5;
// itemsToLoad - number of new items to be displayed
private itemsToLoad: number = 5;
// 18 items loaded for demo purposes
private items = [
{
"Name": "XYz Company0"
},
{
"Name": "XYz Company1"
},
{
"Name": "XYz Company2"
},
{
"Name": "XYz Company3"
},
{
"Name": "XYz Company4"
},
{
"Name": "XYz Company5"
},
{
"Name": "XYz Company6"
},
{
"Name": "XYz Company7"
},
{
"Name": "XYz Company8"
},
{
"Name": "XYz Company9"
},
{
"Name": "XYz Company10"
},
{
"Name": "XYz Company11"
},
{
"Name": "XYz Company12"
},
{
"Name": "XYz Company13"
},
{
"Name": "XYz Company14"
},
{
"Name": "XYz Company15"
},
{
"Name": "XYz Company16"
},
{
"Name": "XYz Company17"
}
];
// List that is going to be actually displayed to user
public itemsToShow = this.items.slice(0, this.noOfItemsToShowInitially);
// No need to call onScroll if full list has already been displayed
public isFullListDisplayed: boolean = false;
onScroll() {
if (this.noOfItemsToShowInitially <= this.items.length) {
// Update ending position to select more items from the array
this.noOfItemsToShowInitially += this.itemsToLoad;
this.itemsToShow = this.items.slice(0, this.noOfItemsToShowInitially);
console.log("scrolled");
} else {
this.isFullListDisplayed = true;
}
}
}
当您向下滚动列表时,您将成功看到警告消息。
以上是上述代码的plunker。