Du kan bruge ngx-infinite-scroll .
npm install ngx-infinite-scroll --save
Se demo plnkr .
I din komponentskabelon:
<div class="search-results"
data-infinite-scroll
debounce
[infiniteScrollDistance]="scrollDistance"
[infiniteScrollUpDistance]="scrollUpDistance"
[infiniteScrollThrottle]="throttle"
(scrolled)="onScrollDown()"
(scrolledUp)="onUp()">
<p *ngFor="let i of array">
{{ i }}
</p>
</div>
I din komponentcontroller:
onScrollDown (ev) {
console.log('scrolled down!!', ev);
// add another 10 items
const start = this.sum;
this.sum += 10;
this.appendItems(start, this.sum);
this.direction = 'down'
}
onUp(ev) {
console.log('scrolled up!', ev);
const start = this.sum;
this.sum += 10;
this.prependItems(start, this.sum);
this.direction = 'up';
}
Dette gøres med en simpel datatjeneste, men du kan implementere en brugerdefineret metode til at hente data fra databasen. For eksempel:
// Page 1
db.comments.find().limit(10)
// Page 2
db.comments.find().skip(10).limit(10)
// Page 3
db.comments.find().skip(10).limit(10)