如何使用ng-class

时间:2015-10-14 17:45:44

标签: angularjs semantic-ui ng-class

我使用语义ui和angular(1.4)ui来构建应用程序。我遇到这样一种情况:通过角度动态添加类名导致错误的渲染,因为ng-class似乎随意重新排列类名。这是不合需要的,因为语义要求某些类名组合遵循自然/语义顺序。例如:

<div class="ui two column grid">
   <div ng-class="wideVersion? 'sixteen wide column': 'fourteen wide column'"></div>
</div>

这个结果(当范围变量wideVersion改变时)在dom:

<div class="ui two column grid">
   <div class="wide column fourteen"></div>
</div>

这不起作用,因为语义要求类名遵循此用例中的自然顺序。我尝试了多个ng级变体但结果是一样的。

由于

1 个答案:

答案 0 :(得分:0)

我认为您可以使用ng-attr-class或仅使用内插插值的类。 但是有些情况下angular会自动向元素添加类,例如ng-show和表单输入,因此您可能需要找到更完整的解决方案。

angular.module('test', [])

.controller('Test', function($scope){
  
});
.test.one.two {
  color:red;
}

.test.three.four {
  color:blue;
}

.test.five.six {
  color:green;
}

.test.seven.eight {
  color:pink;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<div ng-app='test' ng-controller='Test'>
  <div ng-attr-class="{{checked ? 'test one two' : 'test three four'}}">test1</div>
  <div class="{{checked ? 'test five six' : 'test seven eight'}}">test2</div>
  <input type='checkbox' ng-model='checked'> click
</div>