0
0
Angularframework~5 mins

*ngIf for conditional rendering in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the *ngIf directive do in Angular?

*ngIf conditionally adds or removes elements from the DOM based on a true or false expression.

Click to reveal answer
beginner
How do you write *ngIf to show a message only when isLoggedIn is true?
<div *ngIf="isLoggedIn">Welcome back!</div>
Click to reveal answer
beginner
What happens to the element when the *ngIf condition is false?

The element is completely removed from the DOM, not just hidden.

Click to reveal answer
intermediate
How can you provide an else block with *ngIf?

Use *ngIf="condition; else elseBlock" and define a <ng-template #elseBlock> for the else content.

Click to reveal answer
intermediate
Why is *ngIf preferred over CSS display:none for conditional rendering?

Because *ngIf removes elements from the DOM, improving performance and accessibility, while display:none only hides them visually.

Click to reveal answer
What does *ngIf="false" do to the element?
ARemoves it from the DOM
BHides it with CSS
CShows it normally
DThrows an error
How do you add an else block with *ngIf?
A*ngIf="condition" elseBlock
B*ngIfElse="elseBlock"
C*ngIfElseBlock="true"
D*ngIf="condition; else elseBlock" with &lt;ng-template #elseBlock&gt;
Which of these is true about *ngIf?
AIt toggles element visibility with CSS
BIt adds or removes elements from the DOM
CIt only works on <code>&lt;div&gt;</code> elements
DIt requires a function to work
What happens if you use *ngIf on a component?
AThe component is created or destroyed based on the condition
BThe component is always created but hidden
CThe component throws an error
DThe component duplicates
Why might *ngIf improve accessibility?
ABecause it disables keyboard navigation
BBecause it changes font size automatically
CBecause hidden elements are still read by screen readers
DBecause it adds ARIA labels
Explain how *ngIf works for showing or hiding content in Angular.
Think about what happens to the element in the page structure.
You got /3 concepts.
    Describe how to use an else block with *ngIf and why it might be useful.
    Consider how you show something when the condition is false.
    You got /3 concepts.