关于angularjs:使用ng-class添加多个类

Adding multiple class using ng-class

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

例如

1
 

如果是,那么任何人都可以举这个例子。


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

1
    Hello World!

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

1
2
3
<!-- notice expression1 used twice -->

    Hello World!

或者很简单:

1
    Hello World!

注意css类周围的单引号。


是的,您可以使用多个表达式在ng-class中添加多个类。

例如:

1
 Dummy Data


对于三元运算符表示法:

1
 


这是其他答案的强大替代方法:

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

一些例子:

1.只需在div中添加" class1 class2 class3":

1
 

2.根据$ index,向div添加" odd"或" even"类:

1
 

3.根据$ index为每个div动态创建一个类

1
 

如果$index=5,将导致:

1
 

这是您可以运行的代码示例:

1
2
3
4
var app = angular.module('app', []);
app.controller('MyCtrl', function($scope){
  $scope.items = 'abcdefg'.split('');
});
1
2
3
4
.odd  { background-color: #eee; }
.even { background-color: #fff; }
.index5 {background-color: #0095ff; color: white; font-weight: bold; }
* { font-family:"Courier New", Courier, monospace; }
1
2
3
4
5
6
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">


  <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(' ')}}"


使用控制器上的$scope方法,可以计算要在视图中输出的类。如果您具有用于计算类名的复杂逻辑,并且通过将其移至控制器来减少视图中的逻辑量,这尤其方便。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
app.controller('myController', function($scope) {

    $scope.className = function() {

        var className = 'initClass';

        if (condition1())
            className += ' class1';

        if (condition2())
            className += ' class2';

        return className;
    };
});

在视图中,只需:

1
 


您的示例适用于条件类(如果expressionDataX为true,则显示类名):

您还可以添加由元素的用户提供的多个类:

用法:


这是一个使用OR ||比较多个angular-ui-router状态的示例。操作员:

1
2
3
4
5
6
7
8
9
10
<li ng-class="
    {
        warning:
            $state.includes('out.pay.code.wrong')
            || $state.includes('out.pay.failed')
        ,
        active:
            $state.includes('out.pay')
    }
">

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


在active和activemenu下面是类,而itemCount和ShowCart是表达式/布尔值。

1
ng-class="{'active' : itemCount, 'activemenu' : showCart}"

有多个条件

1
Hello World!

通过Scotch.io找到了另一种方法

1
 

这是我的参考。
https://scotch.io/tutorials/the-many-ways-to-use-ngclass


我们可以通过其他方式创建一个函数来控制"使用多个类"

的CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 <style>
    .Red {
        color: Red;
    }
    .Yellow {
        color: Yellow;
    }
      .Blue {
        color: Blue;
    }
      .Green {
        color: Green;
    }
    .Gray {
        color: Gray;
    }
    .b {
         font-weight: bold;
    }
</style>

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    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";
                }
        }]);

使用它

1
2
3
4
5
6
7
8
9
10
11
12
<body ng-app="myapp" ng-controller="ExampleController">

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


</ul>

您可以参考ng-class的完整代码页(例如)