将此多重条件转换为ng级条件

时间:2019-05-10 07:25:19

标签: angularjs ng-class

您好,我需要帮助将具有'or'条件的'if-else'转换为可以在ng-class中使用的条件。

这是我的带条件的ng类,但是无法正常工作。

<span ng-class="{'green': work > toWork,
                 'red': work < toWork,
                 'black': work == toWork || overall == '-'}">
    {{overall = showMonthly(work = (workers | getMonthValue: dts.value),
                            toWork = getMonthlyToWork(member.id, dts.value))}}
</span>

这是我要适用的条件:

if (work > toWork) {
  return "green";
}else if (work < toWork) {
  return "red";
}else if (work == toWork || overall == "-") {
  return "black";
}

2 个答案:

答案 0 :(得分:1)

您不需要ng-class,只需要将逻辑放在$scope中的方法内,如下所示

$scope.getClass = function(work, toWork, overall){
    if (work == toWork || overall == "-"){
      return "black";
    }else if (work < toWork) {
      return "red";
    }else if(work > toWork)  {
      return "green";
    }
}

在您看来,这样称呼

<span class="{{getClass(work, toWork, overall)}}"></span>

答案 1 :(得分:0)

<span ng-class="{'green': work > toWork,
                 'red': work < toWork,
                 'black': (work == toWork || overall == '-')}">
  ...
</span>

检查此。 (您的条件陈述中缺少拼写

编码愉快!