What are Angular directives and how do structural and attribute directives differ?
Learn Angular directives and how structural directives like *ngIf differ from attribute directives like ngClass, with clear examples and a custom directive.
Expected Interview Answer
Angular directives are classes that add behavior to elements in the DOM; structural directives change the DOM layout by adding or removing elements, while attribute directives change the appearance or behavior of an existing element.
Structural directives such as *ngIf, *ngFor and *ngSwitch use the leading asterisk syntax and manipulate the DOM through a template and a ViewContainerRef, physically inserting or removing nodes. Attribute directives such as ngClass, ngStyle and custom directives applied like [appHighlight] operate on an element that already exists, modifying its properties, styles, or event handling. Components are technically directives with a template.
- Encapsulate reusable DOM behavior in one place
- Structural directives keep templates declarative and readable
- Attribute directives centralize styling and interaction logic
- Custom directives extend HTML without new components
- Improve testability by isolating DOM manipulation
AI Mentor Explanation
Structural directives are like the match official deciding whether a batter is on the field at all — sent out to bat or given out and removed from the crease. Attribute directives are the coach who leaves a player on the pitch but changes their stance, grip, or fielding position. One controls presence in the game, the other tweaks how a present player behaves.
Step-by-Step Explanation
Step 1
Recognize the three directive kinds
Components (directives with a template), structural directives (change DOM layout), and attribute directives (change an element's look or behavior).
Step 2
Spot structural directives by the asterisk
The * in *ngIf and *ngFor is sugar for an <ng-template> that Angular adds or removes from the DOM.
Step 3
Apply attribute directives in brackets
Use [ngClass], [ngStyle], or a custom [appHighlight] to modify an element that already exists.
Step 4
Build a custom attribute directive
Create a class with @Directive, inject ElementRef and Renderer2, and use @HostListener or @HostBinding to alter the host element.
Step 5
Choose based on intent
If you need to add or remove elements, reach for a structural directive; if you only need to restyle or add behavior, use an attribute directive.
What Interviewer Expects
- A clear definition of directives
- The distinction: structural changes DOM layout, attribute changes appearance/behavior
- Examples of each (*ngIf/*ngFor vs ngClass/ngStyle)
- Awareness that components are directives with a template
- Knowledge of how to build a custom attribute directive
Common Mistakes
- Thinking *ngIf just hides an element instead of removing it from the DOM
- Confusing attribute directives with plain HTML attributes
- Forgetting the asterisk is syntactic sugar for <ng-template>
- Putting two structural directives on the same element
- Not knowing a component is itself a directive
Best Answer (HR Friendly)
“Directives are Angular instructions that change how elements on a page behave. Structural ones add or remove elements from the page, like showing a section only when a condition is true, while attribute ones change the look or behavior of an element that is already there, like highlighting it.”
Code Example
<!-- Structural: adds/removes DOM -->
<p *ngIf="isLoggedIn">Welcome back!</p>
<li *ngFor="let item of items">{{ item }}</li>
<!-- Attribute: modifies existing element -->
<span appHighlight [ngClass]="{ active: isActive }">Text</span>
@Directive({ selector: '[appHighlight]' })
export class HighlightDirective {
constructor(private el: ElementRef, private r: Renderer2) {}
@HostListener('mouseenter') onEnter() {
this.r.setStyle(this.el.nativeElement, 'background', 'yellow');
}
}Follow-up Questions
- Why can you only apply one structural directive per element?
- How does the asterisk in *ngIf expand to <ng-template>?
- How do @HostBinding and @HostListener work in a custom directive?
- What is the difference between ngClass and the class binding [class.x]?
- How is a component related to a directive?
MCQ Practice
1. Which of these is a structural directive?
*ngFor adds or removes elements from the DOM, which makes it a structural directive.
2. What does an attribute directive typically do?
Attribute directives modify an element that already exists, altering its styles, classes, or behavior.
3. In Angular, a component is best described as...
A component is a special directive that also has its own template and view.
Flash Cards
What is a structural directive? — A directive that changes DOM layout by adding or removing elements, e.g. *ngIf, *ngFor; it uses the asterisk syntax.
What is an attribute directive? — A directive that changes the appearance or behavior of an existing element, e.g. ngClass, ngStyle, or a custom [appHighlight].
What does the asterisk in *ngIf mean? — It is shorthand for wrapping the element in an <ng-template> that Angular conditionally instantiates.
Is a component a directive? — Yes — a component is a directive that additionally has a template.