本文翻译自:AngularJS ngClass conditional
Is there any way to make an expression for something like
For example, I have tried the following: 例如,我尝试了以下方法:
1 | <span ng-class="{test: 'obj.value1 == \'someothervalue\''}">test</span> |
The issue with this code is that no matter what
1 | <span ng-class="{test: obj.value2}">test</span> |
As long as
1 | <span ng-class="{test: checkValue1()}">test</span> |
Where the
1 2 3 | $scope.checkValue1 = function() { return $scope.obj.value === 'somevalue'; } |
I am just wondering if this is how
Here is a plnkr to show what I mean. 这是显示我的意思的plnkr 。
#1楼
参考:https://stackoom.com/question/17MA5/AngularJS-ngClass条件
#2楼
Your first attempt was almost right, It should work without the quotes. 您的第一次尝试几乎是正确的,它应该没有引号。
1 | {test: obj.value1 == 'someothervalue'} |
Here is a plnkr . 这是一个plnkr 。
The ngClass directive will work with any expression that evaluates truthy or falsey, a bit similar to Javascript expressions but with some differences, you can read about here . ngClass指令可与任何评估为真或假的表达式一起使用,这有点类似于Javascript表达式,但有一些区别,您可以在这里阅读。 If your conditional is too complex, then you can use a function that returns truthy or falsey, as you did in your third attempt. 如果条件太复杂,则可以像第三次尝试一样使用返回true或false的函数。
Just to complement: You can also use logical operators to form logical expressions like 只是为了补充:您还可以使用逻辑运算符来形成逻辑表达式,例如
1 | ng-class="{'test': obj.value1 == 'someothervalue' || obj.value2 == 'somethingelse'}" |
#3楼
Angular syntax is to use the : operator to perform the equivalent of an if modifier 角语法是使用:运算符执行与if修饰符等效的操作
1 | <div ng-class="{ 'clearfix' : (row % 2) == 0 }"> |
Add clearfix class to even rows. 将clearfix类添加到偶数行。 Nonetheless, expression could be anything we can have in normal if condition and it should evaluate to either true or false. 尽管如此,如果条件满足,表达式可以是我们在正常情况下可以拥有的任何东西,并且它的值应该为true或false。
#4楼
Using ng-class inside ng-repeat 在ng-repeat中使用ng-class
1 2 3 4 5 6 7 8 9 10 11 12 | <table> <tbody> <tr ng-repeat="task in todos" ng-class="{'warning': task.status == 'Hold' , 'success': task.status == 'Completed', 'active': task.status == 'Started', 'danger': task.status == 'Pending' } "> <td>{{$index + 1}}</td> <td>{{task.name}}</td> <td>{{task.date|date:'yyyy-MM-dd'}}</td> <td>{{task.status}}</td> </tr> </tbody> </table> |
For each status in task.status a different class is used for the row. 对于task.status中的每个状态,该行使用不同的类。
#5楼
Using function with ng-class is a good option when someone has to run complex logic to decide the appropriate CSS class. 当有人必须运行复杂的逻辑来确定适当的CSS类时,将功能与ng-class结合使用是一个不错的选择。
http://jsfiddle.net/ms403Ly8/2/ http://jsfiddle.net/ms403Ly8/2/
HTML: HTML:
1 2 3 4 5 | <div ng-app> <div ng-controller="testCtrl"> <div ng-class="getCSSClass()">Testing ng-class using function</div> </div> </div> |
CSS: CSS:
1 | .testclass { Background: lightBlue} |
JavaScript : JavaScript :
1 2 3 4 5 | function testCtrl($scope) { $scope.getCSSClass = function() { return "testclass "; } } |
#6楼
Angular JS provide this functionality in ng-class Directive . Angular JS在ng-class Directive中提供了此功能。 In which you can put condition and also assign conditional class. 您可以在其中放置条件,也可以分配条件类。 You can achieve this in two different ways. 您可以通过两种不同的方法来实现。
Type 1 类型1
1 | <div ng-class="{0:'one', 1:'two',2:'three'}[status]"></div> |
In this code class will be apply according to value of status value 在此代码类中,将根据状态值应用值
if status value is 0 then apply class one 如果状态值为0,则应用类一
if status value is 1 then apply class two 如果状态值是1,那么申请二级
if status value is 2 then apply class three 如果状态值为2,则应用三级
Type 2 2型
1 | <div ng-class="{1:'test_yes', 0:'test_no'}[status]"></div> |
In which class will be apply by value of status 根据状态值将应用于哪个类别
if status value is 1 or true then it will add class test_yes 如果状态值为1或true ,则将添加类test_yes
if status value is 0 or false then it will add class test_no 如果状态值为0或false ,则将添加类test_no