简体   繁体   中英

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

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

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

Here togglecolor() function returns true or false. yellow and red color not getting applied to the row.

You shouldn't be using {{}} interpolation inside your ng-class directive.

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

Better implementation would be directly return a class from togglecolor method.

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

Another way to use is not using a fucntion simple evaluate the expression in the ng-class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM