2

在 ang-repeat中,我想ng-class根据一些项目参数设置 a 的类名。

我想为每个添加性别年龄对应类的简单示例<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>

不幸的是,它似乎不是那样工作的。

知道如何简单地实现这一目标吗?

4

1 回答 1

4

ng-class如果您想添加多个类,例如您的情况,还支持数组表示法:

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

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

于 2014-03-08T16:27:46.030 回答