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

5 Answers 5

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'"
Sign up to request clarification or add additional context in comments.

3 Comments

2) made my day - unexpected behavior.
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.
Why is it that [ngSwitch] works but *ngSwitch does not? I thought, the * was just syntactic sugar for structural directives? 🤔
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>

Comments

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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.