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

如。

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

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

.


当前回答

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

例如:

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

其他回答

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

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>

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

例如:

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

当不同表达式的值为true时应用不同的类:

<div ng-class="{class1 : expression1, class2 : expression2}">
    Hello World!
</div>

当一个表达式为真时应用多个类:

<!-- notice expression1 used twice -->
<div ng-class="{class1 : expression1, class2 : expression1}">
    Hello World!
</div>

或者很简单:

<div ng-class="{'class1 class2' : expression1}">
    Hello World!
</div>

注意css类周围的单引号。

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

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

有多种条件

<div ng-class="{'class1' : con1 || can2, 'class2' : con3 && con4}">
Hello World!
</div>