question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ngSwitch not using the `*` syntax ?

See original GitHub issue

As ngIf or ngFor, ngSwitch is manipulating the DOM. But, unless I miss something, it can only be called using [ng-switch] and not *ng-switch like we do for ngIf or ngFor.

<div [ng-switch]="value">
  <div *ng-switch-when="'1'">is 1</div>
  <div *ng-switch-when="'2'">is 2</div>
  <div *ng-switch-default>is another value</div>
</div>

Maybe it would make more sense to have something like:

<div *ng-switch="value">
  <div *ng-switch-when="'1'">is 1</div>
  <div *ng-switch-when="'2'">is 2</div>
  <div *ng-switch-default>is another value</div>
</div>

which would be closer to what we do for ngIf or ngFor:

<div *ng-if="test">..</div>
<li *ng-for="#user of users">{{user}}</li>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
diestrincommented, Feb 7, 2016

Really old issue, but I have one related with this. I understand @mhevery point about not having the ngSwitch in the <ul>, but this is my scenario.

<ul>
  <template ngFor #item [ngForOf]="items">
    <li class="some-list-of-classes" *ngSwitchWhen="1">List type 1</li>
    <li class="some-other-list-of-classes" *ngSwitchWhen="2">List type 2</li>
    <li class="some-random-list-of-classes" *ngSwitchDefault>List type 3</li>
  </template>
</ul>

And well, as you know, I can’t use the [ngSwitch] in the <template> created for the ngFor. And can’t put it in the <ul> since I need to switch based on some #item value. So my only option in here, is to create just one <li> using ngClass to change a large list of classes, and then put the ngSwitchWhen in a template. But this doesn’t feel like the right way to do it.

I think the better way for this, would be

<ul>
  <template ngFor #item [ngForOf]="items">
    <template [ngSwitch]="item.value">
      <li class="some-list-of-classes" *ngSwitchWhen="1">List type 1</li>
      <li class="some-other-list-of-classes" *ngSwitchWhen="2">List type 2</li>
      <li class="some-random-list-of-classes" *ngSwitchDefault>List type 3</li>
    </template>
  </template>
</ul>

instead of

<ul>
  <template ngFor #item [ngForOf]="items">
    <li [ngSwitch="item.value"] class="{
      'some-list-of-classes': item.value ===  1,
      'some-other-list-of-classes': item.value === 2,
      'some-random-list-of-classes': item.value !== 1 && item.value !== 2
    }">
      <template [ngSwitchWhen]="1">List type 1</template>
      <template [ngSwitchWhen]="2">List type 2</template>
      <template [ngSwitchDefault]>List type 3</template>
    </li>
  </template>
</ul>

or

<ul>
  <template ngFor #item [ngForOf]="items">
    <li class="some-list-of-classes" *ngIf="item.value === 1">List type 1</li>
    <li class="some-other-list-of-classes" *ngIf="item.value === 2">List type 2</li>
    <li class="some-random-list-of-classes" *ngIf="item.value !== 1 && item.value !== 2">List type 3</li>
  </template>
</ul>
0reactions
angular-automatic-lock-bot[bot]commented, Sep 7, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NgIf & NgSwitch • Angular - codecraft.tv
We define the condition by passing an expression to the directive which is evaluated in the context of its host component. The syntax...
Read more >
Angular2 ngSwitch not working - Stack Overflow
Your syntax for using isn't correct. It should be [ngSwitch]="switch_expression" and then the cases should look like this <some-element ...
Read more >
NgSwitch - Angular
The [ngSwitch] directive on a container specifies an expression to match against. The expressions to match are provided by ngSwitchCase directives on views ......
Read more >
ngSwitch, ngSwitchcase, ngSwitchDefault Angular Example
In this tutorial, we will look at the syntax of ngSwitch , ngSwitchcase & ngSwitchDefault . We will show you how to use...
Read more >
Angular 8 ngSwitch Directive - Javatpoint
In Angular 8, ngSwitch is a structural directive which is used to Add/Remove DOM Element. It is similar to switch statement of C#....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found