What if your app could scroll through thousands of items instantly without freezing?
Why Virtual scrolling for large lists in Angular? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a long list of hundreds or thousands of items on a webpage, like a contact list or product catalog. You try to show them all at once by rendering every item in the browser.
Loading and rendering all those items at once makes the page slow and unresponsive. Scrolling becomes laggy, and the browser might even freeze or crash because it tries to handle too much data at the same time.
Virtual scrolling only renders the items visible on the screen plus a small buffer. As you scroll, it dynamically adds new items and removes those that go out of view. This keeps the page fast and smooth no matter how large the list is.
for (let item of items) { render(item); }<cdk-virtual-scroll-viewport> <div *cdkVirtualFor="let item of items">{{item}}</div> </cdk-virtual-scroll-viewport>It enables smooth, fast scrolling through huge lists without slowing down the app or overwhelming the browser.
Think of scrolling through your phone contacts app. Even if you have thousands of contacts, it feels instant and smooth because it only shows a few contacts at a time, loading more as you scroll.
Rendering all list items at once can crash or slow down your app.
Virtual scrolling renders only visible items to keep performance high.
This technique makes large lists feel fast and responsive.
Practice
cdk-virtual-scroll-viewport in Angular for large lists?Solution
Step 1: Understand virtual scrolling purpose
Virtual scrolling aims to improve performance by rendering only the visible items in a large list.Step 2: Identify
This Angular component implements virtual scrolling by showing only the items visible in the viewport, reducing DOM load.cdk-virtual-scroll-viewportroleFinal Answer:
It renders only visible items to improve performance. -> Option AQuick Check:
Virtual scrolling = render visible items only [OK]
- Thinking it sorts or animates items
- Assuming it duplicates items for speed
- Confusing with pagination
Solution
Step 1: Identify correct directive for virtual scroll
Angular CDK virtual scroll requires*cdkVirtualForinstead of*ngForto render visible items only.Step 2: Check syntax and itemSize usage
TheitemSizeattribute is required oncdk-virtual-scroll-viewportto set fixed item height for scrolling calculations.Final Answer:
<cdk-virtual-scroll-viewport itemSize="50"><div *cdkVirtualFor="let item of items">{{item}}</div></cdk-virtual-scroll-viewport> -> Option CQuick Check:
Use *cdkVirtualFor with itemSize on viewport [OK]
- Using *ngFor instead of *cdkVirtualFor
- Using 'let item in items' instead of 'of'
- Omitting itemSize attribute
<cdk-virtual-scroll-viewport itemSize="40" style="height: 120px;">
<div *cdkVirtualFor="let item of items">{{item}}</div>
</cdk-virtual-scroll-viewport>and
items = ['A', 'B', 'C', 'D', 'E'], how many items will be visible at once?Solution
Step 1: Calculate viewport height in pixels
The viewport height is set to 120px.Step 2: Divide viewport height by itemSize
Each item is 40px tall, so 120px / 40px = 3 items visible at once.Final Answer:
3 items -> Option AQuick Check:
Visible items = viewport height / itemSize = 3 [OK]
- Counting all items instead of visible ones
- Ignoring style height on viewport
- Using wrong division operator
<cdk-virtual-scroll-viewport itemSize="50">
<div *cdkVirtualFor="let item in items">{{item}}</div>
</cdk-virtual-scroll-viewport>Solution
Step 1: Check syntax of *cdkVirtualFor directive
The correct syntax uses 'of' to iterate over items, not 'in'. Using 'in' causes a template error.Step 2: Verify other attributes
itemSize can be a string representing a number, and style height is recommended but not syntax error. The directive must be on the repeated element, not viewport.Final Answer:
Using *cdkVirtualFor with 'in' instead of 'of' causes an error. -> Option DQuick Check:
*cdkVirtualFor uses 'of', not 'in' [OK]
- Using 'in' instead of 'of' in *cdkVirtualFor
- Forgetting to set viewport height
- Placing directive on wrong element
Solution
Step 1: Understand virtual scroll requirements
Virtual scrolling requires a fixeditemSizeto calculate visible items and smooth scrolling.Step 2: Use correct directives and styles
Usecdk-virtual-scroll-viewportwith a fixed height and*cdkVirtualForto render only visible items efficiently.Step 3: Avoid common pitfalls
Not settingitemSizeor using*ngForcauses performance issues or errors.Final Answer:
Use cdk-virtual-scroll-viewport with fixed itemSize, set viewport height, and use *cdkVirtualFor. -> Option BQuick Check:
Fixed itemSize + *cdkVirtualFor + viewport height = smooth scroll [OK]
- Omitting itemSize or viewport height
- Using *ngFor instead of *cdkVirtualFor
- Trying variable item sizes without special handling
