# NgSwitch

The Angular NgSwitch is actually a set of cooperating directives: `NgSwitch`, `NgSwitchCase`, and `NgSwitchDefault`.

```markup
src/app/app.component.html (ngswitch)

<div [ngSwitch]="hero?.emotion">
  <app-happy-hero    *ngSwitchCase="'happy'"    [hero]="hero"></app-happy-hero>
  <app-sad-hero      *ngSwitchCase="'sad'"      [hero]="hero"></app-sad-hero>
  <app-confused-hero *ngSwitchCase="'app-confused'" [hero]="hero"></app-confused-hero>
  <app-unknown-hero  *ngSwitchDefault           [hero]="hero"></app-unknown-hero>
</div>
```

* **NgSwitch**
  * itself is not a structural directive.&#x20;
  * It's an **attribute directive** that controls the behavior of the other two switch directives. That's why you write \[ngSwitch], never \*ngSwitch.
  * The emotion value in this example is a string, but the switch value can be of any type.
* **NgSwitchCase** & **NgSwitchDefault**
  * **structural directives**.&#x20;
  * You attach them to elements using the asterisk (\*) prefix notation.&#x20;
  * An NgSwitchCase displays its host element when its value matches the switch value.&#x20;
  * The NgSwitchDefault displays its host element when no sibling NgSwitchCase matches the switch value.


---

# 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://fancyloves.gitbook.io/frontendtech/angularjs/fundamentals/directives/ngswitch.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.
