<ul class="list">
<cdk-virtual-scroll-viewport style="height: 500px" itemSize="90" >
<div *ngFor="let n of numbers" style="height:130px;">{{n}}</div>
</cdk-virtual-scroll-viewport>
</ul>
<!--app.module.ts-->
import { ScrollingModule } from '@angular/cdk/scrolling';
@NgModule({
imports: [ ScrollingModule ]
})
<!--app.component.ts-->
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
numbers: number[] = [];
constructor() {
for (let index = 0; index < 10000; index++) {
this.numbers.push(index);
}
}
}
Everything is fine but its showing "=====>Can't bind to 'cdkVirtualForOf' since it isn't a known property of 'div'.<=====" ERROR
You should import ScrollingModule:
import { ScrollingModule } from '@angular/cdk/scrolling';
And add it into inports array in NgModule:
@NgModule({
...
imports: [
ScrollingModule
],
...
})
Now add some styles to your div:
.example-viewport {
height: 200px;
width: 200px;
border: 1px solid black;
}
.example-item {
height: 50px;
}
It works for me) good luck)