我以前没有见过这种特定的语法,背后的理由是{true: 'warning', false: 'ok'}[scores.Indicator == 'Negative']
什么?
我ngClass
在这里使用的方式是
<tr ng-repeat="scores in Test" ng-class="{warning: (scores.Indicator == 'Negative'), ok: (scores.Indicator != 'Negative')}">
那样有用吗?
为了更好的可读性,您也可以将其委托给控制器
<tr ng-repeat="scores in Test" ng-class="scoreClass(scores)">
$scope.scoreClass = function(scores) {
return scores.Indicator == 'Negative' ? 'warning': 'ok';
}
或者你可以创建一个指令
<tr ng-repeat="scores in Test" score-class scores="scores">
.directive('scoreClass', [function() {
return {
restrict: 'A',
scope: {
scores: '=',
},
link: function(scope, element, attrs, controller) {
scope.$watch('scores', function() {
element.removeClass('ok');
element.removeClass('warning');
if (scope.scores.Indicator == 'Negative') {
element.addClass('warning');
} else {
element.addClass('ok');
}
}, true);
}
};
}]);