0

当 i.typ 更改时,ng-switch 不会在下面的代码中更新。也许这与开关之外的 ng-repeat 有关?在页面重新加载时它有效,但在模型更改时无效。

<div id="sceneCtrl" ng-controller="SceneController">

    <transform id="{{'annotation'+i.ID}}" ng-repeat="i in vm.sharedService.SceneAnnotations">
        <group ng-switch on="{{i.typ}}">
            <transform ng-switch-when="0" > [subtree 0...] </transform>
            <transform ng-switch-when="1" > [subtree 1...] </transform>
        </group>
    </transform>

</div>
4

2 回答 2

2

我整理了一个示例,说明 ng-switch 在 ng-repeat 中的外观。为了使示例工作,我确实替换了您的变量,但想法和语法完全相同:

<div ng-repeat="player in myPlayers">
  <!-- just so you can see the repeat is working normally: -->
  Player: <span class="playername" ng-bind="player.name"></span>

  <!-- here is the switch: -->
  <div ng-switch on="player.gameRole">
    <div ng-switch-when="IT">YOU ARE IT</div>
    <div ng-switch-when="NOT IT">This player is NOT it</div>
  </div>
  <br /><br />
</div>

要现场观看,请在此处查看 plunker:http://plnkr.co/edit/0lbqKjlgo1Y8aLNMoGNs?p= preview


至于您的另一个问题,即为什么当变量的值发生变化时开关不会改变,有几个原因可能会发生这种情况。最常见的原因是因为您处于指令中或某个“$scope.$apply()”没有被调用的地方。添加这一行,它应该可以工作(可能)!

祝你好运!

于 2014-03-19T12:31:21.900 回答
2

从您的on="{{i.typ}}".

试试这个:

<group ng-switch on="i.typ">
于 2014-03-19T12:20:39.753 回答