0
0
Angularframework~5 mins

*ngFor for list rendering in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of *ngFor in Angular?

*ngFor is a directive used to repeat a block of HTML for each item in a list or array. It helps display lists dynamically.

Click to reveal answer
beginner
How do you write a basic *ngFor loop to display items from an array called items?
<pre>&lt;li *ngFor="let item of items"&gt;{{ item }}&lt;/li&gt;</pre><p>This repeats the &lt;li&gt; element for each <code>item</code> in <code>items</code>.</p>
Click to reveal answer
beginner
What does the keyword <code>let</code> mean in <code>*ngFor="let item of items"</code>?

let declares a local variable item that holds the current element from the items array during each iteration.

Click to reveal answer
intermediate
How can you access the index of the current item in an *ngFor loop?
<pre>&lt;li *ngFor="let item of items; let i = index"&gt;{{ i }} - {{ item }}&lt;/li&gt;</pre><p>The <code>index</code> keyword gives the zero-based position of the current item.</p>
Click to reveal answer
advanced
Why is it important to use trackBy with *ngFor in large lists?

trackBy helps Angular identify items uniquely to optimize rendering and avoid unnecessary DOM updates, improving performance.

Click to reveal answer
What does *ngFor="let item of items" do in Angular?
ADeletes items from the array
BCreates a new component for each item
CRepeats the HTML element for each item in the items array
DSorts the items array
How do you get the current index inside an *ngFor loop?
AUsing <code>let i = index</code>
BUsing <code>let i = position</code>
CUsing <code>let i = count</code>
DUsing <code>let i = number</code>
What is the role of trackBy in *ngFor?
ATo sort items alphabetically
BTo filter items in the list
CTo add animations to list items
DTo uniquely identify items for better rendering performance
Which of these is the correct syntax to loop over an array users with *ngFor?
A<code>*ngFor="users let user"</code>
B<code>*ngFor="let user of users"</code>
C<code>*ngFor="user for users"</code>
D<code>*ngFor="users in user"</code>
What happens if you forget to use let in *ngFor?
AAngular will throw a syntax error
BThe loop will run but variables will be undefined
CThe loop will run normally
DAngular will ignore the loop
Explain how *ngFor works to render lists in Angular. Include how to access the current item and its index.
Think about how you would show a list of names with their position numbers.
You got /3 concepts.
    Describe why using trackBy with *ngFor can improve performance in Angular applications.
    Imagine updating a long list where only one item changes.
    You got /3 concepts.