*ngFor helps you show many items on the screen by repeating a template for each item in a list. It saves time and keeps your code clean.
*ngFor for list rendering in Angular
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>The *ngFor directive repeats the <li> element for each item in the items array.
let item creates a variable for each element in the list during the loop.
fruits array.<ul>
<li *ngFor="let fruit of fruits">{{ fruit }}</li>
</ul><ul> <li *ngFor="let user of users; index as i">{{ i + 1 }}. {{ user.name }}</li> </ul>
trackBy to optimize rendering when the list changes.<ul>
<li *ngFor="let color of colors; trackBy: trackByColor">{{ color }}</li>
</ul>*ngFor does not render any elements.<ul>
<li *ngFor="let item of emptyList">No items found.</li>
</ul>This component shows a heading and a list of fruits. The *ngFor repeats the <li> for each fruit in the fruits array.
import { Component } from '@angular/core'; @Component({ selector: 'app-fruit-list', template: ` <h2>Fruit List</h2> <ul> <li *ngFor="let fruit of fruits">{{ fruit }}</li> </ul> ` }) export class FruitListComponent { fruits = ['Apple', 'Banana', 'Cherry']; }
Time complexity: Rendering takes time proportional to the number of items in the list.
Space complexity: Uses space for each repeated element in the DOM.
Common mistake: forgetting to use let before the variable name in *ngFor.
Use *ngFor when you want to display lists dynamically. For static content, just write HTML directly.
*ngFor repeats a template for each item in a list.
Use let item of items to access each element.
It helps keep your HTML clean and dynamic.
Practice
*ngFor="let item of items" do in a template?Solution
Step 1: Understand the directive purpose
The*ngFordirective is used to repeat an HTML element for each item in a list.Step 2: Analyze the syntax
The syntaxlet item of itemsmeans for each element in the list 'items', create a copy of the element with 'item' as the current element.Final Answer:
It repeats the HTML element for each item in the list called 'items'. -> Option AQuick Check:
*ngFor repeats elements = A [OK]
- Thinking *ngFor modifies the list itself
- Confusing *ngFor with event binding
- Assuming it creates only one element
*ngFor to loop over a list named fruits?Solution
Step 1: Recall correct Angular syntax
The correct syntax for*ngForuseslet variable of listto iterate over the list.Step 2: Compare options
*ngFor="let fruit of fruits"useslet fruit of fruits, which matches the correct syntax.*ngFor="let fruit in fruits"uses 'in' which is incorrect in Angular templates.Final Answer:
*ngFor="let fruit of fruits" -> Option DQuick Check:
Use 'of' not 'in' in *ngFor syntax = B [OK]
- Using 'in' instead of 'of'
- Omitting 'let' keyword
- Swapping variable and list positions
items = ['apple', 'banana', 'cherry'];
And the template:
<ul>
<li *ngFor="let fruit of items">{{ fruit }}</li>
</ul>What will be the rendered HTML output?
Solution
Step 1: Understand the list and template
The list 'items' contains three strings: 'apple', 'banana', and 'cherry'. The template uses*ngForto create a- for each fruit.
Step 2: Render each item
Each- will display the current fruit's name. So the output is three
- elements with the fruit names inside a <ul>.
Final Answer:
<ul><li>apple</li><li>banana</li><li>cherry</li></ul> -> Option CQuick Check:
*ngFor creates one element per list item = C [OK]
- Expecting variable names instead of values
- Thinking *ngFor outputs the list name
- Missing the repeated elements
<div *ngFor="item of items">{{ item }}</div>Solution
Step 1: Check *ngFor syntax
The correct syntax requires 'let' before the loop variable, so it should be*ngFor="let item of items".Step 2: Verify other parts
The interpolation {{ item }} is correct, and *ngFor can be used on any element including <div>. The question does not mention if 'items' is defined, so we assume it is.Final Answer:
The variable declaration is missing 'let' before 'item'. -> Option BQuick Check:
Always use 'let' before variable in *ngFor = A [OK]
- Omitting 'let' keyword
- Assuming *ngFor needs special elements
- Confusing interpolation syntax
users = [{name: 'Alice'}, {name: ''}, {name: 'Bob'}, {name: null}];How can you use
*ngFor to render only users with a non-empty, non-null name?Solution
Step 1: Understand filtering in template
Angular's*ngFordoes not support filtering directly. We use*ngIfinside the loop to conditionally show elements.Step 2: Check conditions for non-empty names
Using*ngIf="user.name"filters out empty strings and null values because they are falsy in Angular templates.Step 3: Compare options
*ngFor="let user of users" <div *ngIf="user.name">{{ user.name }}</div>correctly uses*ngIf="user.name"inside the loop.*ngFor="let user of users" <ng-container *ngIf="user.name">{{ user.name }}</ng-container>has invalid syntax mixing*ngForand*ngIfon the same element.*ngFor="let user of users | filter: user.name"uses a non-existent filter pipe.*ngFor="let user of users" <div *ngIf="user.name !== ''">{{ user.name }}</div>misses filtering null values.Final Answer:
*ngFor="let user of users" <div *ngIf="user.name">{{ user.name }}</div> -> Option AQuick Check:
Use *ngIf inside *ngFor to filter falsy names = D [OK]
- Trying to filter list directly in *ngFor
- Using invalid syntax combining *ngFor and *ngIf
- Not filtering null values properly
Ready to prove it?
Pass the test and this pattern moves unknown
