Oam Psy
Oam Psy

Reputation: 8663

AngularJS - Basic ng-repeat with ng-switch

I have a simple ng-repeat, that displays a list of statuses. I wanted to extend this ng-repeat to ng-switch to display a value depending on the status.

Current ng-repeat:

<td ng-repeat="myStatus in history.Status.slice(0, 12)">
    {{ myStatus }}
</td>

With ng-switch:

<td ng-repeat="myStatus in history.Status.slice(0, 12)" ng-switch="myStatus">
    <span ng-switch-when="1">test1</span>
    <span ng-switch-when="2">test2</span>
    <span ng-switch-when="3">test3</span>
    <span ng-switch-when="4">test4</span>
    <span ng-switch-when="5">test5</span>
    <span ng-switch-when="6">test6</span>
    <span ng-switch-when="7">test7</span>
    <span ng-switch-default>Error</span>
</td>

However, witht eh above ng-switch, nothing is displayed.

Upvotes: 0

Views: 159

Answers (2)

msapkal
msapkal

Reputation: 8346

Works at my end, here is the Plunker

However as a suggestion i would prefer to use a function expression instead of ng-switch.

<td ng-repeat="myStatus in history.Status.slice(0, 12)">
    <span>{{getStatus(myStatus)}}</span>
</td>

getStatus is a function which would return the required information.

Upvotes: 1

Harutyun Abgaryan
Harutyun Abgaryan

Reputation: 2023

Use This

<td ng-repeat="myStatus in history.Status.slice(0, 12)" >
  <span ng-switch="myStatus">
    <span ng-switch-when="1">test1</span>
    <span ng-switch-when="2">test2</span>
    <span ng-switch-when="3">test3</span>
    <span ng-switch-when="4">test4</span>
    <span ng-switch-when="5">test5</span>
    <span ng-switch-when="6">test6</span>
    <span ng-switch-when="7">test7</span>
    <span ng-switch-default>Error</span>
  </span>
</td>

Upvotes: 1

Related Questions