AngularJS - ng-switch v2
HTML
<div ng-controller="MyController">
Value read from global variable: {{const1}}
<p ng-repeat="item in items">
<span ng-switch on="condition(item.value)">
<span ng-switch-when="const1">
<span class="blue">{{item.name}}</span> (emphasis applied by ng-switch)
</span>
<span ng-switch-when="const2">
<span class="green">{{item.name}}</span> (emphasis applied by ng-switch)
</span>
<span ng-switch-when="const3">
<span class="pink">{{item.name}}</span> (emphasis applied by ng-switch)
</span>
<span ng-switch-when="4">
<span class="red">{{item.name}}</span> (emphasis applied by ng-switch)
</span>
<span ng-switch-default>
{{item.name}}
</span>
</span>
</p>
</div>
CSS
.red { color: red; }
.blue { color: blue; }
.green { color: green }
.pink { color: pink }
JavaScript
var app = angular.module('myApp', []);
app.run(function ($rootScope) {
$rootScope.const1 = 1;
$rootScope.const2 = 3;
$rootScope.const3 = 5;
$rootScope.condition = function (value) {
return ["const1", "const2", "const3"].filter(function (name) {
return $rootScope[name] === value;
})[0] || value;
};
});
function MyController($scope) {
$scope.items = [
{ name: "one", value: 1 },
{ name: "two", value: 2 },
{ name: "three", value: 3 },
{ name: "four", value: 4 },
{ name: "five", value: 5 }
];
}