8

I have an ngSwitch for a model attribute bound to a drop-down. It wasn't working, so I tried to simply hard-code the value. Still doesn't work, it displays both divs. What am I doing wrong? Apologies in advance if it's something obvious, I'm new to Angular2.

My html template:

      <!-- display closed date if status is closed, otherwise display active date -->
      <div ngSwitch="ACTV">
          <div class="form-group row" ngSwitchWhen="CLSD">
            <label for="closeDt" class="col-md-4 form-control-label text-md-right">
                                        Close Date
                                        <span class="help-block">Required field</span>
                                    </label>
            <div class="col-md-4 col-xs-12">
              <datetime [timepicker]="false" [(ngModel)]="date2" id="close-date" name="close-date"></datetime>
            </div>
          </div>
          <div class="form-group row" ngSwitchWhen="ACTV">
            <label for="issueDt" class="col-md-4 form-control-label text-md-right">
                                        Active Date
                                        <span class="help-block">Required field</span>
                                    </label>
            <div class="col-md-4 col-xs-12">
              <datetime [timepicker]="false" [(ngModel)]="date2" id="active-date" name="active-date"></datetime>
            </div>
          </div>
      </div>

Result on the npm server:

enter image description here

Arlo Guthrie
  • 1,152
  • 3
  • 12
  • 28

5 Answers5

23

In case DEMO required : https://plnkr.co/edit/SCZC5Cx9gnQbg1AkkspX?p=preview

Change,

1)

ngSwitch="ACTV"        TO     [ngSwitch]="'ACTV'"

2)

ngSwitchWhen="CLSD"    TO     *ngSwitchCase="'CLSD'"

3)

ngSwitchWhen="ACTV"    To     *ngSwitchCase="'ACTV'"
micronyks
  • 54,797
  • 15
  • 112
  • 146
  • 4
    2) made my day - unexpected behavior. – Dima Dec 14 '17 at 14:18
  • 1
    This answer is helpful (it helped me!), but I suspect #1 is incorrect (not what the OP or others will want to do). If the switch expression is a string literal ('ACTV'), then the switch will always match the 2nd case. The first case will never show. I suspect that ACTV is a variable that can have values 'ACTV' or 'CLSD'. IOW, remove the single quotes from #1. ... It is confusing that the variable has the same name as one its possible values. Maybe the variable should have a different name. – steve Jan 30 '19 at 15:13
  • Why is it that `[ngSwitch]` works but `*ngSwitch` does not? I thought, the `*` was just syntactic sugar for structural directives? – Florian Gössele Jun 01 '22 at 15:29
6

What version of angular2 are you using? In the final (release) version the syntax that works for me is:

<div [ngSwitch]="someVariable">
  <div *ngSwitchCase="value1">...</div>
  <div *ngSwitchCase="value2">...</div>
</div>
Meir
  • 14,081
  • 4
  • 39
  • 47
2

Your syntax for using isn't correct. It should be [ngSwitch]="switch_expression" and then the cases should look like this <some-element *ngSwitchCase="match_expression_1">

See here for how to use it.

Katana24
  • 8,706
  • 19
  • 76
  • 118
1

you have to test on object attributs

switch_expression { 
     match_expression_1: value1, 
     match_expression_2: value2, 
     match_expression_3: value3,  
}

and then :

<div [ngSwitch]="switch_expression">
   <div  *ngSwitchCase="match_expression_1">...</div>
   <div  *ngSwitchCase="match_expression_2">...</div>
</div>

for more informations : https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html

Mourad Idrissi
  • 3,455
  • 5
  • 17
  • 29
0

If the variable you are switching on gets updated via subscription, try adding async pipe:

<div [ngSwitch]="someVariable" | async>

https://angular.io/docs/ts/latest/api/common/index/AsyncPipe-pipe.html