Angular2 ngSwitch not working
Asked Answered
N

5

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

Nonpros answered 21/10, 2016 at 15:21 Comment(0)
S
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>
Samul answered 21/10, 2016 at 15:25 Comment(0)
G
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'"
Gustav answered 21/10, 2016 at 15:28 Comment(3)
2) made my day - unexpected behavior.Joletta
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.Balefire
Why is it that [ngSwitch] works but *ngSwitch does not? I thought, the * was just syntactic sugar for structural directives? 🤔Mammalogy
S
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>
Samul answered 21/10, 2016 at 15:25 Comment(0)
S
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.

Subcritical answered 21/10, 2016 at 15:25 Comment(0)
R
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

Raver answered 21/10, 2016 at 15:28 Comment(0)
R
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

Religieux answered 26/1, 2017 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.