When you first start working with Angular, directives can feel confusing. Why do some directives add or remove elements from the DOM while others just change styles or behavior?
The reason is simple: Angular divides directives into two major categories — Structural and Attribute directives.
This article explains the difference with real examples, practical use-cases, do/don’t guidelines, and comparison charts.
1. What Are Directives in Angular?
A directive is a class that can modify the DOM or the behavior of a DOM element/component.
Angular has three types of directives:
Components (a template + logic)
Structural Directives (
*ngIf,*ngFor,*ngSwitch)Attribute Directives (
ngClass,ngStyle,ngModel, custom attributes)
This article focuses on Structural vs Attribute directives.
2. Structural Directives — They Change the DOM Structure
Structural directives add, remove, or manipulate DOM elements.
They always start with an asterisk * because Angular transforms the syntax internally.
Examples
*ngIf*ngFor*ngSwitchCase*ngSwitchDefault
How Structural Directives Work
They shape your DOM by creating or destroying elements.
Example using *ngIf:
<div *ngIf="isLoggedIn">
Welcome back!
</div>
If isLoggedIn = false, Angular removes the entire <div> from the DOM.
Case Study Example
Imagine a dashboard that only shows “Admin Panel” to admin users:
<div *ngIf="user.role === 'admin'">
<app-admin-panel></app-admin-panel>
</div>
This directive determines whether the element exists or not.
When to Use Structural Directives
Conditionally showing UI
Rendering lists
Dynamically shaping layout
Switching views
3. Attribute Directives — They Modify Existing Elements
Attribute directives do not add or remove elements.
Instead, they simply change the appearance or behavior of an existing element.
Examples
ngClassngStylengModelCustom attribute directive (highlight, auto-focus, etc.)
How Attribute Directives Work
<div [ngClass]="{ 'active': isActive }">
Menu Item
</div>
The <div> element remains in the DOM — only its styling changes.
Case Study Example
Highlight an input when the value is invalid:
<input [ngStyle]="{ borderColor: isInvalid ? 'red' : 'grey' }" />
When to Use Attribute Directives
Changing CSS classes
Applying dynamic styles
Handling user interactions
Adding custom behaviors (e.g., auto-focus, hover effects)

Join the conversation! Your thoughts help the community grow.