AngularJS - 根据数字应用ng-class

时间:2014-07-23 13:14:26

标签: angularjs angularjs-scope ng-class

我理解如何将ng-class用于布尔值,例如:

<div ng-class="{true: 'success', false: 'error'}[IsPositive]">

但是,我如何根据数值执行?

例如,如果我有一个$ scope.myNumber,可以是任何正值或负值,例如

-212

-31

0

31

123

如何使用ng-class应用3种类型,即:

$scope.myNumber > 0
$scope.myNumber < 0
$scope.myNumber == 0

3 个答案:

答案 0 :(得分:3)

<div ng-class="{'zero' : myNumber == 0, 
'negative' : myNumber < 0, 
'positive' : myNumber > 0}">

答案 1 :(得分:3)

示例:

 <div ng-class="{'one': myNumber > 5 && myNumber < 10, 'two' : myNumber > 10, 'three' : myNumber < 5, 'equal' : myNumber === 0}"></div>

你可以看到现场: http://plnkr.co/edit/zdaN53?p=preview

答案 2 :(得分:1)

试试这个?

<div ng-class="{'green':myNumber>0,'red':myNumber<0,'orange':myNumber==0}"> </div>