> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/web-frameworks/angular/directives/structural-directives.md).

# 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>
```
