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