# Structural Directives

* Change the DOM layout by adding or removing DOM elements.

## Built-in Structural Directives

<table><thead><tr><th width="138.57142857142856">Directives</th><th>Details</th></tr></thead><tbody><tr><td><code>ngFor</code></td><td>Repeat a node for each item in a list.</td></tr><tr><td><code>ngIf</code></td><td>Conditionally creates or disposes of subviews from the template.</td></tr><tr><td><code>ngSwitch</code></td><td>A set of directives that switch among alternative views.</td></tr></tbody></table>

### ngFor

```javascript
<div *ngFor="let item of items">{{item.name}}</div>
```

* repeating a component view

```javascript
<app-item-detail *ngFor="let item of items" [item]="item"></app-item-detail>
```

### ngIf

* When `NgIf` is `false`, Angular removes an element and its descendants from the DOM. Angular then disposes of their components, which frees up memory and resources.

```javascript
<app-item-detail *ngIf="isActive" [item]="item"></app-item-detail>
```

* guarding against `null`

```javascript
<div *ngIf="currentCustomer">Hello, {{currentCustomer.name}}</div>
```

### ngSwitch

<table><thead><tr><th width="204">Directives</th><th>Details</th></tr></thead><tbody><tr><td><code>ngSwitch</code></td><td>An attribute directive that changes the behavior of its companion directives.</td></tr><tr><td><code>ngSwitchCase</code></td><td>Structural directive that adds its element to the DOM when its bound value equals the switch value and removes its bound value when it doesn't equal the switch value.</td></tr><tr><td><code>ngSwitchDefault</code></td><td>Structural directive that adds its element to the DOM when there is no selected <code>ngSwitchCase</code>.</td></tr></tbody></table>

```javascript
<div [ngSwitch]="currentItem.feature">
  <app-stout-item *ngSwitchCase="'stout'" [item]="currentItem"></app-stout-item>
  <app-device-item *ngSwitchCase="'slim'" [item]="currentItem"></app-device-item>
  <app-lost-item *ngSwitchCase="'vintage'" [item]="currentItem"></app-lost-item>
  <app-best-item *ngSwitchCase="'bright'" [item]="currentItem"></app-best-item>
<!-- . . . -->
  <app-unknown-item *ngSwitchDefault [item]="currentItem"></app-unknown-item>
</div>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dailyjournal.gitbook.io/notes/web-frameworks/angular/directives/structural-directives.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
