0
0
Angularframework~20 mins

*ngIf for conditional rendering in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NgIf Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be rendered with this *ngIf condition?
Consider this Angular template snippet:
<div *ngIf="isVisible">Hello World</div>

If isVisible is false, what will be rendered in the browser?
Angular
<div *ngIf="isVisible">Hello World</div>
AAn empty div will be rendered with no text inside.
BThe text 'Hello World' will be shown.
CThe div will be rendered but hidden with CSS.
DNothing will be rendered; the div is removed from the DOM.
Attempts:
2 left
💡 Hint
Think about what *ngIf does when the condition is false.
📝 Syntax
intermediate
2:00remaining
Which *ngIf syntax is correct for showing content only if count is greater than 0?
Choose the correct Angular template syntax to conditionally render a paragraph only when count > 0.
A<p *ngIf="count &gt; 0">Count is positive</p>
B<p *ngIf="count &lt; 0">Count is positive</p>
C<p *ngIf="count == 0">Count is positive</p>
D<p *ngIf="count = 0">Count is positive</p>
Attempts:
2 left
💡 Hint
Remember the comparison operator for 'greater than' is >.
🔧 Debug
advanced
2:00remaining
Why does this *ngIf condition cause an error?
Given this Angular template:
<div *ngIf="user.isLoggedIn">Welcome!</div>

and the component has user = null;, what error will Angular throw?
Angular
user = null;
ANo error; the div will not render.
BSyntaxError: Unexpected token '.'
CTypeError: Cannot read property 'isLoggedIn' of null
DReferenceError: user is not defined
Attempts:
2 left
💡 Hint
What happens if you try to access a property on null in JavaScript?
state_output
advanced
2:00remaining
What is the rendered output after toggling the flag?
Given this component code:
flag = true;

and template:
<button (click)="flag = !flag">Toggle</button>
<div *ngIf="flag">Visible</div>

What will the user see after clicking the button twice?
Angular
flag = true;
AAn error occurs because flag is undefined.
BThe text 'Visible' will be shown.
CThe button disappears.
DNothing will be shown.
Attempts:
2 left
💡 Hint
Each click toggles the flag between true and false.
🧠 Conceptual
expert
2:00remaining
Why prefer *ngIf over CSS display:none for conditional rendering?
Which is the best reason to use Angular's *ngIf directive instead of toggling CSS display:none to hide/show elements?
A*ngIf removes the element from the DOM, improving performance and accessibility.
B*ngIf only hides the element visually but keeps it in the DOM for faster toggling.
CCSS display:none removes the element from the DOM, which can cause errors.
DUsing CSS display:none is preferred because it triggers Angular change detection.
Attempts:
2 left
💡 Hint
Think about what happens in the DOM and how screen readers behave.