Function call in ng-class attribute of ng-repeat directive

Can anyone figure out the problem with the following line of code.

<tr ng-repeat="myrecord in myData" 
  ng-class="togglecolor('{{myrecord.name}}','{{myrecord[$index-1].name}}')? 'yellow':'red'">

      

Here the togglecolor () function returns true or false. yellow and red are not applied to the line.

+3


source to share


2 answers


You shouldn't use interpolation {{}}

inside your directive ng-class

.

ng-class="togglecolor(myrecord.name,myrecord[$index-1].name)? 'yellow':'red'"

      


A better implementation would directly return a class from a method togglecolor

.



ng-class="togglecolor(myrecord.name,myrecord[$index-1].name)"

      

code

$scope.togglecolor = togglecolor;
function togglecolor(name, prevName){
   var flag;
   //some awesome logic set flag value here, either true/false
   return flag ? 'yellow': 'red';
}

      

+2


source


Another way to use is not to use fucntion, just evaluate the expression in the ng class



<tr ng-repeat="myrecord in myData" 
   ng-class="{true:'yellow', false:'red'} [toggleColor]>

      

+1


source







All Articles