What is the best way to conditionally apply a class?

后端 未结 22 2583
感动是毒
感动是毒 2020-11-22 09:12

Lets say you have an array that is rendered in a ul with an li for each element and a property on the controller called selectedIndex.

22条回答
  •  [愿得一人]
    2020-11-22 09:22

    I'll add to this, because some of these answers seem out of date. Here's how I do it:

    
    

    Where 'isSelected' is a javascript variable defined within the scoped angular controller.


    To more specifically address your question, here's how you might generate a list with that:

    HTML

  • {{item.name}}

  • JS

    function ListCtrl($scope) {    
        $scope.list = [  
            {"name": "Item 1", "isSelected": "active"},  
            {"name": "Item 2", "isSelected": ""}
        ]
    }
    


    See: http://jsfiddle.net/tTfWM/

    See: http://docs.angularjs.org/api/ng.directive:ngClass

提交回复
热议问题