Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Virtual scrolling for large lists
📖 Scenario: You are building a web app that shows a long list of user names. To keep the app fast and smooth, you want to show only the visible part of the list at a time. This technique is called virtual scrolling.
🎯 Goal: Create an Angular standalone component that uses virtual scrolling to display a list of 1000 user names efficiently.
📋 What You'll Learn
Create a list of 1000 user names as strings
Set a page size variable to control how many items show at once
Use Angular's cdk-virtual-scroll-viewport to render the list with virtual scrolling
Display each user name inside the virtual scroll viewport
💡 Why This Matters
🌍 Real World
Virtual scrolling is used in apps that show long lists like contacts, messages, or product catalogs to keep the app fast and responsive.
💼 Career
Knowing how to implement virtual scrolling is important for frontend developers to optimize performance and user experience in real-world Angular applications.
Progress0 / 4 steps
1
Create the user list
Create a variable called users that is an array of strings containing exactly these three names: 'Alice', 'Bob', and 'Charlie'.
Angular
Hint
Use square brackets to create an array and separate names with commas.
2
Add a page size variable
Add a variable called pageSize and set it to 10. This will control how many users show at once in the virtual scroll viewport.
Angular
Hint
Just create a new variable with the number 10.
3
Generate 1000 user names
Replace the users array with a new array of 1000 strings. Each string should be 'User ' followed by the user number from 1 to 1000. Use Array.from with a mapping function to create this array.
Angular
Hint
Use Array.from({ length: 1000 }, (_, i) => `User ${i + 1}`) to create the array.
4
Add virtual scroll viewport in template
Create an Angular standalone component named UserListComponent. In its template, add a cdk-virtual-scroll-viewport with a height of 200px and item size of 20. Inside it, use an *ngFor to display each user from the users array in a div. Import ScrollingModule from @angular/cdk/scrolling and include it in the component's imports array.
Angular
Hint
Use Angular's cdk-virtual-scroll-viewport with itemSize and fixed height. Use *ngFor inside it to show users.
Practice
(1/5)
1. What is the main benefit of using cdk-virtual-scroll-viewport in Angular for large lists?
easy
A. It renders only visible items to improve performance.
B. It automatically sorts the list items alphabetically.
C. It adds animations to list items when scrolling.
D. It duplicates list items for faster loading.
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 cdk-virtual-scroll-viewport role
This Angular component implements virtual scrolling by showing only the items visible in the viewport, reducing DOM load.
Final Answer:
It renders only visible items to improve performance. -> Option A
Quick Check:
Virtual scrolling = render visible items only [OK]
Hint: Virtual scroll shows visible items only to boost speed [OK]
Common Mistakes:
Thinking it sorts or animates items
Assuming it duplicates items for speed
Confusing with pagination
2. Which of the following is the correct way to use virtual scrolling in Angular template?
easy
A. <cdk-virtual-scroll-viewport><div *cdkVirtualFor="let item in items">{{item}}</div></cdk-virtual-scroll-viewport>
B. <cdk-virtual-scroll-viewport itemSize="50"><div *ngFor="let item of items">{{item}}</div></cdk-virtual-scroll-viewport>
C. <cdk-virtual-scroll-viewport itemSize="50"><div *cdkVirtualFor="let item of items">{{item}}</div></cdk-virtual-scroll-viewport>
D. <cdk-virtual-scroll-viewport itemSize="50"><div *ngFor="let item in items">{{item}}</div></cdk-virtual-scroll-viewport>
Solution
Step 1: Identify correct directive for virtual scroll
Angular CDK virtual scroll requires *cdkVirtualFor instead of *ngFor to render visible items only.
Step 2: Check syntax and itemSize usage
The itemSize attribute is required on cdk-virtual-scroll-viewport to 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 C
Quick Check:
Use *cdkVirtualFor with itemSize on viewport [OK]
Hint: Use *cdkVirtualFor, not *ngFor, inside cdk-virtual-scroll-viewport [OK]
Common Mistakes:
Using *ngFor instead of *cdkVirtualFor
Using 'let item in items' instead of 'of'
Omitting itemSize attribute
3. Given this Angular template snippet:
<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?
medium
A. 3 items
B. 5 items
C. 2 items
D. 4 items
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.
Hint: Divide viewport height by itemSize to find visible items [OK]
Common Mistakes:
Counting all items instead of visible ones
Ignoring style height on viewport
Using wrong division operator
4. What is wrong with this Angular virtual scroll code?
<cdk-virtual-scroll-viewport itemSize="50">
<div *cdkVirtualFor="let item in items">{{item}}</div>
</cdk-virtual-scroll-viewport>
medium
A. The *cdkVirtualFor directive must be on the viewport, not the div.
B. Missing style height on viewport causes no scroll.
C. itemSize must be a number, not a string.
D. Using *cdkVirtualFor with 'in' instead of 'of' causes an error.
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 D
Quick Check:
*cdkVirtualFor uses 'of', not 'in' [OK]
Hint: Use 'of' with *cdkVirtualFor, not 'in' [OK]
Common Mistakes:
Using 'in' instead of 'of' in *cdkVirtualFor
Forgetting to set viewport height
Placing directive on wrong element
5. You want to display a list of 10,000 items with virtual scrolling. Which combination of settings ensures smooth scrolling and correct rendering?
hard
A. Use *ngFor inside a scrollable div without cdk-virtual-scroll-viewport.
B. Use cdk-virtual-scroll-viewport with fixed itemSize, set viewport height, and use *cdkVirtualFor.
C. Use cdk-virtual-scroll-viewport with variable itemSize and *cdkVirtualFor.
D. Use cdk-virtual-scroll-viewport without itemSize, and *ngFor for rendering.
Solution
Step 1: Understand virtual scroll requirements
Virtual scrolling requires a fixed itemSize to calculate visible items and smooth scrolling.
Step 2: Use correct directives and styles
Use cdk-virtual-scroll-viewport with a fixed height and *cdkVirtualFor to render only visible items efficiently.
Step 3: Avoid common pitfalls
Not setting itemSize or using *ngFor causes performance issues or errors.
Final Answer:
Use cdk-virtual-scroll-viewport with fixed itemSize, set viewport height, and use *cdkVirtualFor. -> Option B