<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) {switch(strValue) {case "It is Red":return "Red";break;case "It is Yellow":return "Yellow";break;case "It is Blue":return "Blue";break;case "It is Green":return "Green";break;case "It is Gray":return "Gray";break;}}}]);</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><hr/><p>Other way using : ng-class="{'class1' : expression1, 'class2' : expression2,'class3':expression2,...}"</p><ul><li ng-repeat="icolor in MyColors"><p ng-class="{'Red':icolor=='It is Red','Yellow':icolor=='It is Yellow','Blue':icolor=='It is Blue','Green':icolor=='It is Green','Gray':icolor=='It is Gray'}" class="b">\{\{icolor}}</p></li></ul>
// Support for string argumentsgetClassNames('class1', 'class2');
// support for ObjectgetClassNames({class1: true, class2 : false});
// support for all type of datagetClassNames('class1', 'class2', ['class3', 'class4'], {class5 : function() { return false; },class6 : function() { return true; }});
<div className={getClassNames({class1: true, class2 : false})} />