Structural Directives
Learn how Angular structural directives control the creation, removal, and repetition of elements in the DOM.
What are Structural Directives?
Structural directives are Angular directives that change the structure of the DOM by adding, removing, or repeating elements.
They are commonly used for conditional rendering and displaying collections of data.
Structural Directive Flow
Condition
Angular evaluates a condition or collection.
Directive
The structural directive processes the expression.
DOM
Angular creates or removes template content.
UI
The required content appears on the page.
Common Structural Directives
*ngIf
Displays an element when a condition is true.
*ngFor
Repeats an element for every item in a collection.
*ngSwitch
Displays content based on a selected case.
Control Flow
Modern Angular also provides built-in control-flow syntax.
Using *ngIf
*ngIf conditionally adds or removes an element
from the DOM.
isLoggedIn = true;
<div *ngIf="isLoggedIn">
Welcome to CIIT Training Institute
</div>
isLoggedIn is true.
Using *ngIf with else
An alternative template can be displayed when the condition is false.
<div *ngIf="isLoggedIn; else loginMessage">
Welcome Student
</div>
<ng-template #loginMessage>
Please Login
</ng-template>
Using *ngFor
*ngFor repeats an element for every item in
an array.
students = [
'Pradnya',
'Rahul',
'Amit'
];
<ul>
<li *ngFor="let student of students">
{{student}}
</li>
</ul>
Using Index with *ngFor
The index value can be used to display the
position of each item.
<div *ngFor="let student of students; let i = index">
{{i + 1}} - {{student}}
</div>
2 - Rahul
3 - Amit
Using *ngSwitch
*ngSwitch can be used when different content
needs to be displayed according to a value.
course = 'Angular';
<div [ngSwitch]="course">
<p *ngSwitchCase="'Angular'">
Angular Course
</p>
<p *ngSwitchCase="'Java'">
Java Course
</p>
<p *ngSwitchDefault>
Course Not Found
</p>
</div>
How Structural Directives Affect the DOM
Create
Angular creates template content when required.
Remove
Angular removes content when a condition is false.
Repeat
Angular repeats content for collection items.
Switch
Angular selects content according to a value.
Modern Angular Control Flow
Modern Angular also provides built-in control-flow syntax
such as @if, @for, and
@switch.
@if (isLoggedIn) {
<p>Welcome Student</p>
}
@for (student of students; track student) {
<p>{{student}}</p>
}
Structural Directive Comparison
| Directive | Purpose | Example |
|---|---|---|
*ngIf |
Conditional rendering | *ngIf="isLoggedIn" |
*ngFor |
Repeat elements | *ngFor="let student of students" |
*ngSwitch |
Multiple conditions | [ngSwitch]="course" |
@if |
Modern conditional rendering | @if (condition) |
@for |
Modern repeated rendering | @for (item of items) |
Complete Student Example
export class StudentComponent {
isLoggedIn = true;
students = [
'Pradnya',
'Rahul',
'Amit'
];
}
<h3 *ngIf="isLoggedIn">
Welcome to Angular Training
</h3>
<ul>
<li *ngFor="let student of students">
{{student}}
</li>
</ul>
Welcome to Angular Training
Students:
Pradnya | Rahul | Amit
Best Practices
- Use conditional rendering only when necessary.
- Use tracking when rendering large collections.
- Keep structural directive expressions simple.
- Use modern Angular control flow in new Angular applications where appropriate.
- Avoid unnecessary nested structural directives.
- Keep template logic readable and maintainable.
Summary
- Structural directives change the structure of the DOM.
*ngIfprovides conditional rendering.*ngForrepeats content for collection items.*ngSwitchselects content based on a value.- Modern Angular provides
@if,@for, and@switch. - Structural directives are useful for dynamic UI rendering.