Search code examples
angularjsng-class

can ng-class be set by function call


Is it possible to use a function name to designate the ng-class ? The reason I ask is because I have bugginess in my green/red colorations and I've tried many ways to fix it. If this trick SHOULDNT work then I know I can try something else. If it should work, then I still cannot explain why my tables don't color as they should.

This is currently in my code, and works the same bugginess as my other code.

<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;
};

Solution

  • You don't need the curly brackets in your ng-class property but otherwise it is perfectly viable:

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

    I would definitely check your comparators in your if statements. Depending on the data types of your variables they may not behave the way you would expect.