2016-04-01 131 views
1

是否可以使用函数名来指定ng类?我问的原因是因为我的绿色/红色着色力很差,我尝试了很多方法来修复它。如果这个技巧SHOULDNT工作,那么我知道我可以尝试其他的东西。如果它应该起作用,那么我仍然无法解释为什么我的桌子不应该有颜色。ng函数可以通过函数调用

这是目前在我的代码中,和我的其他代码工作相同的bugginess。

<td ng-class="GetColor({{$index}})">{{ps.pval2}}</td> 




$scope.GetColor = function(z) {   //returns color string for use in ng-class 
    $scope.tmp = 'red'; 
    if($scope.PlayerStats[z].pval2.valueOf() > $scope.PlayerStats[z].pval3) { 
     tmp = 'green'; 
    } else if($scope.PlayerStats[z].pval2.valueOf() == $scope.PlayerStats[z].pval3){ 
      tmp = 'yellow'; 
    } else { 
      tmp = 'red'; 
    } 

    return tmp; 
}; 

回答

1

你不需要在你的NG-类属性的大括号但除此之外,它是完全可行的:

<td ng-class="GetColor($index)">{{ps.pval2}}</td> 

我肯定会检查你的比较在你的if语句。根据变量的数据类型,它们可能不像您期望的那样行事。

+0

相同的答案在几秒钟内值得+1 :) – Lex

+0

如果我能+2你的答案我会只是成为一个赢家。 – Gremash

2

是的,这应该工作,但你不需要在$index左右花括号。刚:

<td ng-class="GetColor($index)">{{ps.pval2}}</td> 
+2

我想我们在同一时间点击提交... – Gremash

相关问题