I am new in angular 4. I want to implement scroll pagination in angular 4. Initially I want to show 20 records. After scroll down I want to show next 20. I will do the same till end of list.
I tried to implement it using "angular2-infinite-scroll". but I am not able to show initially first 20 records as well scroll data.
component file :
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 file :
<div
infinite-scroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="10"
(scrolled)="onScroll()"
>
<p *ngFor="let items of item">
{{items.Name}}
</p>
</div>
If anyone having about it please share same.
I recommend using ngx-infinite-scroll against
angular2-infinite-scrolldue to new features and compatibility with AOT.First you need to specify the
scrollWindowproperty if you are not using the whole window and usingoverflow: scrollproperty to emulate a scrollable div. Also in yourScrollComponentclass you need to haveitemas a property of class and not a variable, so you should usepublic iteminstead oflet item.If the size of list is already known than you can leverage
infiniteScrollDisabledto improve performance.Also as it is grammatically incorrect to name multiple things as
itemand call single thing anitem. I am going to changeitemtoitems( You can see that in the ngFor loop in template html )You will now successfully see an alert message when you scroll down the list.
Here is a working plunker of above code.