typescript
45 lines · 6 steps
Virtual scrolling a contact list in Angular
An OnPush standalone component renders thousands of contacts efficiently by combining the CDK virtual scroll viewport with a signal.
Explained by
highlit
1import { Component, ChangeDetectionStrategy, inject, signal } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { ScrollingModule } from '@angular/cdk/scrolling';
4import { toObservable } from '@angular/core/rxjs-interop';
5import { ContactService, Contact } from './contact.service';
6
7@Component({
8 selector: 'app-contact-list',
9 standalone: true,
10 changeDetection: ChangeDetectionStrategy.OnPush,
11 imports: [CommonModule, ScrollingModule],
12 styles: [`
13 .viewport { height: 480px; width: 320px; }
14 .row {
15 display: flex;
16 align-items: center;
17 gap: 12px;
18 height: 56px;
19 padding: 0 16px;
20 border-bottom: 1px solid var(--divider);
21 }
22 .avatar { width: 32px; height: 32px; border-radius: 50%; }
23 .name { font-weight: 500; }
24 .email { color: var(--muted); font-size: 0.85rem; }
25 `],
26 template: `
27 <cdk-virtual-scroll-viewport itemSize="56" minBufferPx="280" maxBufferPx="560" class="viewport">
28 <div *cdkVirtualFor="let contact of contacts(); trackBy: trackById" class="row">
29 <img class="avatar" [src]="contact.avatarUrl" [alt]="contact.name" loading="lazy" />
30 <div>
31 <div class="name">{{ contact.name }}</div>
32 <div class="email">{{ contact.email }}</div>
33 </div>
34 </div>
35 </cdk-virtual-scroll-viewport>
36 `,
37})
38export class ContactListComponent {
39 private readonly contactService = inject(ContactService);
40 readonly contacts = signal<Contact[]>(this.contactService.getAll());
41
42 trackById(_index: number, contact: Contact): string {
43 return contact.id;
44 }
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1CDK virtual scrolling renders only the visible window, so list size stops dictating DOM cost.
- 2A signal reads cleanly under OnPush change detection, making template updates explicit and cheap.
- 3A trackBy keyed on stable identity is what lets recycled rows update without full re-creation.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express'; import { DataSource } from 'typeorm';
Per-tenant database connections in NestJS
multi-tenancy
connection-pooling
dependency-injection
Advanced
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/virtual-scrolling-a-contact-list-in-angular-explained-typescript-df91/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.