我们可以有多个表达式来添加多个ng-class吗?

如。

<div ng-class="{class1: expressionData1, class2: expressionData2}"></div>

如果是,谁能举个例子这样做。

.


当前回答

多亏了Scotch.io,我找到了另一种方法

<div ng-repeat="step in steps" class="step-container step" ng-class="[step.status, step.type]" ng-click="onClick(step.type)">

这是我的推荐信。路径

其他回答

对于三元运算符表示法:

<div ng-class="expression1? 'class1 class2' : 'class3 class4'">

下面是一个使用OR ||操作符比较多个angular-ui-路由器状态的例子:

<li ng-class="
    {
        warning:
            $state.includes('out.pay.code.wrong')
            || $state.includes('out.pay.failed')
        ,
        active:
            $state.includes('out.pay')
    }
">

它将根据条件是否满足,向li类发出警告和/或激活。

这里有一个非常强大的替代答案:

ng-class="[  { key: resulting-class-expression }[ key-matching-expression ], ..  ]"

一些例子:

1. 简单地将'class1 class2 class3'添加到div:

<div ng-class="[{true: 'class1'}[true], {true: 'class2 class3'}[true]]"></div>

2. 将'odd'或'even'类添加到div中,这取决于$index:

<div ng-class="[{0:'even', 1:'odd'}[ $index % 2]]"></div>

3.基于$index动态地为每个div创建一个类

<div ng-class="[{true:'index'+$index}[true]]"></div>

如果$index=5,这将导致:

<div class="index5"></div>

下面是一个你可以运行的代码示例:

var app = angular.module('app', []); app.controller('MyCtrl', function($scope){ $scope.items = 'abcdefg'.split(''); }); .odd { background-color: #eee; } .even { background-color: #fff; } .index5 {background-color: #0095ff; color: white; font-weight: bold; } * { font-family: "Courier New", Courier, monospace; } <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script> <div ng-app="app" ng-controller="MyCtrl"> <div ng-repeat="item in items" ng-class="[{true:'index'+$index}[true], {0:'even', 1:'odd'}[ $index % 2 ]]"> index {{$index}} = "{{item}}" ng-class="{{[{true:'index'+$index}[true], {0:'even', 1:'odd'}[ $index % 2 ]].join(' ')}}" </div> </div>

另一种方法是创建一个函数来控制"使用多个类"

CSS

 <style>
    .Red {
        color: Red;
    }
    .Yellow {
        color: Yellow;
    }
      .Blue {
        color: Blue;
    }
      .Green {
        color: Green;
    }
    .Gray {
        color: Gray;
    }
    .b {
         font-weight: bold;
    }
</style>

脚本

<script>
    angular.module('myapp', [])
            .controller('ExampleController', ['$scope', function ($scope) {
                $scope.MyColors = ['It is Red', 'It is Yellow', 'It is Blue', 'It is Green', 'It is Gray'];
                $scope.getClass = function (strValue) {
                    if (strValue == ("It is Red"))
                        return "Red";
                    else if (strValue == ("It is Yellow"))
                        return "Yellow";
                    else if (strValue == ("It is Blue"))
                        return "Blue";
                    else if (strValue == ("It is Green"))
                        return "Green";
                    else if (strValue == ("It is Gray"))
                        return "Gray";
                }
        }]);
</script>

使用它

<body ng-app="myapp" ng-controller="ExampleController">

<h2>AngularJS ng-class if example</h2>
<ul >
    <li ng-repeat="icolor in MyColors" >
        <p ng-class="[getClass(icolor), 'b']">{{icolor}}</p>
    </li>
</ul>

你可以参考ng-class的完整代码页if example

是的,你可以在ng-class中有多个表达式来添加多个类。

例如:

<div ng-class="{class1:Result.length==2,class2:Result.length==3}"> Dummy Data </div>