Angularjs ng-class如何混合条件和数组(切换)类选择

时间:2014-12-08 09:53:55

标签: angularjs

我有一个选定的类和一些类,取决于var

{selected_class : model.is_selected}

{on:'on_class', off:'off_class', wait: 'wait_class'}[model.state]

如何在ng-class指令中混合这两个?

2 个答案:

答案 0 :(得分:0)

喜欢这个。我不知道"聪明的"方式:

ng-class="{selected_class : model.is_selected, 
  on_class: model.state=='on',
  off_class: model.state=='off',
  wait_class: model.state=='wait'}"

答案 1 :(得分:0)

如果您需要应用多个类,则可以编写更长的ng-class表达式:

ng-class="{'selected_class': model.is_selected, 'on_class': model.state == 'on', 'off_class': model.state == 'off', 'wait_class': model.state == 'wait'}