0
0
Angularframework~5 mins

Structural vs attribute directives in Angular - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is a structural directive in Angular?
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
Which directive type changes the style or behavior of an element without changing the DOM?
AAttribute directive
BStructural directive
CPipe
DModule
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
Which of these is an example of an attribute directive?
AngClass
B*ngIf
CngModule
D*ngFor
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
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.