How can I use the $index inside a ng-repeat to enable a class and show a DIV?

后端 未结 2 1283
感情败类
感情败类 2020-12-02 05:14

I have a set of

  • elements.

  • 2条回答
    •  时光取名叫无心
      2020-12-02 05:44

      The issue here is that ng-repeat creates its own scope, so when you do selected=$index it creates a new a selected property in that scope rather than altering the existing one. To fix this you have two options:

      Change the selected property to a non-primitive (ie object or array, which makes javascript look up the prototype chain) then set a value on that:

      $scope.selected = {value: 0};
      
      A{{$index}}
      

      See plunker

      or

      Use the $parent variable to access the correct property. Though less recommended as it increases coupling between scopes

      A{{$index}}
      

      See plunker

    提交回复
    热议问题