AngularJS - 使用ng-switch进行基本ng-repeat

时间:2014-10-10 09:20:10

标签: angularjs angularjs-scope angularjs-ng-repeat ng-repeat ng-switch

我有一个简单的ng-repeat,它显示了一个状态列表。我想将此ng-repeat扩展到ng-switch以根据状态显示值。

当前ng-repeat:

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

使用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>

但是,如果在ng-switch之上,则不会显示任何内容。

2 个答案:

答案 0 :(得分:1)

使用此

<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>

答案 1 :(得分:1)

在我的最后工作,这是 Plunker

然而,作为一个建议,我宁愿使用函数表达式而不是ng-switch。

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

getStatus是一个可以返回所需信息的函数。