【问题标题】:Ng-switch doesn't work inside ng-repeatng-switch 在 ng-repeat 中不起作用
【发布时间】:2014-03-19 12:18:45
【问题描述】:

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

【问题讨论】:

    标签: angularjs ng-repeat ng-switch


    【解决方案1】:

    on="{{i.typ}}" 中删除大括号。

    试试这个:

    <group ng-switch on="i.typ">
    

    【讨论】:

      【解决方案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()”没有被调用的地方。添加这一行,它应该可以工作(可能)!

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2013-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多