A structural directive changes the layout by adding or removing elements in the DOM. It controls the structure of the view, like *ngIf or *ngFor.
Click to reveal answer
beginner
What is an attribute directive in Angular?
An attribute directive changes the appearance or behavior of an existing element without changing the DOM structure. For example, ngClass or ngStyle.
Click to reveal answer
beginner
How does *ngIf work as a structural directive?
*ngIf adds or removes elements from the DOM based on a condition. If the condition is false, the element is removed completely.
Click to reveal answer
beginner
Can attribute directives add or remove elements from the DOM?
No, attribute directives only change how elements look or behave but do not add or remove elements from the DOM.
Click to reveal answer
intermediate
Give an example of when to use a structural directive vs an attribute directive.
Use a structural directive like *ngFor to repeat elements or *ngIf to show/hide elements. Use an attribute directive like ngStyle to change colors or styles without changing the element itself.
Click to reveal answer
Which Angular directive changes the DOM structure by adding or removing elements?
AComponent
BAttribute directive
CStructural directive
DService
✗ Incorrect
Structural directives control the DOM layout by adding or removing elements.
Which directive type changes the style or behavior of an element without changing the DOM?
AAttribute directive
BStructural directive
CPipe
DModule
✗ Incorrect
Attribute directives modify appearance or behavior but do not change the DOM structure.
What does *ngIf do in Angular?
AChanges the style of an element
BAdds or removes elements based on a condition
CRepeats elements in a list
DDefines a new component
✗ Incorrect
*ngIf conditionally adds or removes elements from the DOM.
Which of these is an example of an attribute directive?
AngClass
B*ngIf
CngModule
D*ngFor
✗ Incorrect
ngClass changes the CSS classes of an element without changing the DOM.
Can attribute directives remove elements from the DOM?
AYes, always
BOnly if used with components
COnly in Angular 17+
DNo, they only change appearance or behavior
✗ Incorrect
Attribute directives do not remove or add elements; they only change how elements look or behave.
Explain the difference between structural and attribute directives in Angular.
Think about whether the directive adds/removes elements or just changes styles.
You got /4 concepts.
Describe a scenario where you would use a structural directive instead of an attribute directive.
When the page layout changes, structural directives are the choice.
You got /3 concepts.
Practice
(1/5)
1. Which of the following best describes a structural directive in Angular?
easy
A. It binds data to an element's property.
B. It changes the style of an element without altering the DOM structure.
C. It changes the layout by adding or removing elements from the DOM.
D. It listens to user events like clicks or key presses.
Solution
Step 1: Understand what structural directives do
Structural directives modify the DOM layout by adding or removing elements dynamically.
Step 2: Compare with other directive types
Attribute directives change appearance or behavior but do not add or remove elements.
Final Answer:
It changes the layout by adding or removing elements from the DOM. -> Option C
Thinking *ngIf and attribute bindings cannot be combined
Confusing *ngIf with [ngIf]
Replacing attribute bindings with structural directive syntax
5. You want to create a custom directive that highlights text by changing its background color when hovered. Which type of directive should you create and why?
hard
A. Structural directive, because it needs to add or remove elements on hover.
B. Attribute directive, because it changes the element's appearance without altering layout.
C. Structural directive, because it modifies the DOM structure dynamically.
D. Attribute directive, because it listens to user events but does not change styles.
Solution
Step 1: Understand the directive's purpose
The directive changes background color on hover, affecting appearance only.
Step 2: Choose directive type based on behavior
Attribute directives change element style or behavior without adding/removing elements, fitting this case.
Final Answer:
Attribute directive, because it changes the element's appearance without altering layout. -> Option B
Quick Check:
Appearance change = attribute directive [OK]
Hint: Appearance changes use attribute directives, layout changes use structural [OK]
Common Mistakes:
Choosing structural directive for style changes
Confusing event listening with directive type
Thinking attribute directives add/remove elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.