What is the best way to conditionally apply a class?

后端 未结 22 2224
感动是毒
感动是毒 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:17

    My favorite method is using the ternary expression.

    ng-class="condition ? 'trueClass' : 'falseClass'"
    

    Note: Incase you're using a older version of Angular you should use this instead,

    ng-class="condition && 'trueClass' || 'falseClass'"
    
    0 讨论(0)
  • 2020-11-22 09:20

    This will probably get downvoted to oblivion, but here is how I used 1.1.5's ternary operators to switch classes depending on whether a row in a table is the first, middle or last -- except if there is only one row in the table:

    <span class="attribute-row" ng-class="(restaurant.Attributes.length === 1) || ($first ? 'attribute-first-row': false || $middle ? 'attribute-middle-row': false || $last ? 'attribute-last-row': false)">
    </span>
    
    0 讨论(0)
  • 2020-11-22 09:20

    Here is another option that works well when ng-class can't be used (for example when styling SVG):

    ng-attr-class="{{someBoolean && 'class-when-true' || 'class-when-false' }}"
    

    (I think you need to be on latest unstable Angular to use ng-attr-, I'm currently on 1.1.4)

    0 讨论(0)
  • 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:

    <class="ng-class:isSelected">
    

    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

    <div ng-controller="ListCtrl">  
        <li class="ng-class:item.isSelected" ng-repeat="item in list">   
           {{item.name}}
        </li>  
    </div>
    


    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

    0 讨论(0)
  • 2020-11-22 09:24

    This works like a charm ;)

    <ul class="nav nav-pills" ng-init="selectedType = 'return'">
        <li role="presentation" ng-class="{'active':selectedType === 'return'}"
            ng-click="selectedType = 'return'"><a href="#return">return
    
        </a></li>
        <li role="presentation" ng-class="{'active':selectedType === 'oneway'}"
            ng-click="selectedType = 'oneway'"><a href="#oneway">oneway
        </a></li>
    </ul>
    
    0 讨论(0)
  • 2020-11-22 09:26

    I am new to Angular but have found this to solve my issue:

    <i class="icon-download" ng-click="showDetails = ! showDetails" ng-class="{'icon-upload': showDetails}"></i>
    

    This will conditionally apply a class based on a var. It starts off with a icon-download as a default, the using ng-class, I check the status of showDetails if true/false and apply class icon-upload. Its working great.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题