0
0
Angularframework~20 mins

Structural vs attribute directives in Angular - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Identify the type of directive
Which of the following Angular directives is a structural directive that changes the DOM layout by adding or removing elements?
A*ngIf
B[ngStyle]
C(click)
D[ngClass]
Attempts:
2 left
💡 Hint
Structural directives usually start with an asterisk (*) and control the presence of elements.
component_behavior
intermediate
1:30remaining
Effect of attribute directive on element
Given this Angular template snippet:
<div [ngClass]="{ 'highlight': isActive }">Hello</div>

What happens when isActive is true?
AThe <code>div</code> element is removed from the DOM.
BThe <code>highlight</code> CSS class is added to the <code>div</code> element.
CThe <code>div</code> element is replaced with a <code>span</code> element.
DThe <code>div</code> element's inline style changes to <code>color: red</code>.
Attempts:
2 left
💡 Hint
Attribute directives change appearance or behavior without changing the DOM structure.
📝 Syntax
advanced
1:30remaining
Correct syntax for structural directive
Which option shows the correct Angular syntax to conditionally display a paragraph using a structural directive?
A<p *ngIf="showParagraph">This is a paragraph.</p>
B<p [ngIf]="showParagraph">This is a paragraph.</p>
C<p (ngIf)="showParagraph">This is a paragraph.</p>
D<p #ngIf="showParagraph">This is a paragraph.</p>
Attempts:
2 left
💡 Hint
Structural directives use an asterisk (*) before their name.
🔧 Debug
advanced
2:00remaining
Why does this structural directive cause an error?
Consider this Angular template code:
<div *ngFor="let item of items">{{ item }}</div>
<div *ngFor="items">{{ item }}</div>

Why does the second div cause an error?
ABecause *ngFor only works with arrays of numbers.
BBecause *ngFor cannot be used on <code>div</code> elements.
CBecause <code>items</code> is not defined in the component.
DBecause *ngFor requires a <code>let</code> variable declaration to iterate over the list.
Attempts:
2 left
💡 Hint
Check the syntax required by *ngFor to loop over a list.
state_output
expert
2:00remaining
Output after toggling structural directive
Given this Angular component template and initial state:
<button (click)="show = !show">Toggle</button>
<div *ngIf="show">Visible</div>

Initially, show is false. What is the rendered output after clicking the button twice?
AThe <code>div</code> with text 'Visible' is shown after two clicks.
BAn error occurs because *ngIf cannot toggle.
CNo <code>div</code> is visible after two clicks.
DThe <code>div</code> is visible only after the first click, then disappears after the second.
Attempts:
2 left
💡 Hint
Think about how toggling a boolean twice affects the condition.