Infinite/virtual scroll for Angular
npm install ngx-ui-scroll-cost

npm install ngx-ui-scroll
javascript
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UiScrollModule } from 'ngx-ui-scroll';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
UiScrollModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
`
$3
Basic usage template may look like
`html
`
where the viewport is a scrollable area of finite height.
`css
.viewport {
height: 300px;
overflow-y: auto;
overflow-anchor: none;
}
`
\uiScroll acts like \ngFor, but the datasource is an object of special type (IDatasource) that can be imported to the host component from UiScrollModule. It implements method _get_ to be used by the \*uiScroll directive to access the data by _index_ and _count_ parameters.
`javascript
import { IDatasource } from 'ngx-ui-scroll';
export class AppComponent {
public datasource: IDatasource = {
get: (index, count, success) => {
const data = [];
for (let i = index; i <= index + count - 1; i++) {
data.push({ text: 'item #' + i });
}
success(data);
}
};
}
`
_Datasource.get_ must provide an array of _count_ data-items started from _index_ position. _Datasource.get_ has 3 signatures: callback based, Promise based and Observable based. So, if you want some remote API to be a source of your data, basically it may look like
`javascript
public datasource: IDatasource = {
get: (index, count) =>
this.http.get(${myApiUrl}?index=${index}&count=${count})
};
`
More details could be found on the DEMO page.
$3
There are some npm scripts available from package.json:
- npm start to run demo App on port 4200
- npm test to run Karma tests
- npm run build to build the ngx-ui-scroll module into the ./dist folder
- npm run install-package` to build tar-gzipped version of package and install it locally into ./node_modules