Question

In a ng-repeat, I'd like to set the class names of a ng-class depending on some item parameters.

Simple example where I wanted to add the gender and the age corresponding class to each <li>:

<div ng-init="friends = [
  {name:'John', age:25, gender:'boy'},
  {name:'Jessie', age:30, gender:'girl'},
  {name:'Johanna', age:28, gender:'girl'},
  {name:'Joy', age:15, gender:'girl'},
  {name:'Mary', age:28, gender:'girl'},
  {name:'Peter', age:95, gender:'boy'},
  {name:'Sebastian', age:50, gender:'boy'},
  {name:'Erika', age:27, gender:'girl'},
  {name:'Patrick', age:40, gender:'boy'},
  {name:'Samantha', age:60, gender:'girl'}
]">
  I have {{friends.length}} friends. They are:
  <input type="search" ng-model="q" placeholder="filter friends..." />
  <ul class="example-animate-container">
    <li class="animate-repeat" ng-repeat="friend in friends | filter:q" ng-class="friend.gender friend.name">
      [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
    </li>
  </ul>
</div>

Unfortunately, it doesn't seem to work that way.

Any idea of how to simply achieve that ?

Was it helpful?

Solution

ng-class also supports array notation if you want to add multiple classes like in your case:

ng-class="[friend.gender, friend.name]"

Demo: http://plnkr.co/edit/c6NIaF2dO5pmJxbj1WRT?p=preview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top