基于ng-repeat内的数组的随机值

时间:2015-10-23 17:50:10

标签: angularjs angularjs-ng-repeat ionic

我需要将4个不同的类添加到div内的ng-repeat

<li ng-repeat="post in posts">
    <span class="color-RANDOM-VALUE">{{post.title}}</span>
</li>

一旦循环运行,它应该看起来像:

<li>
    <span class="color-blue">Title</span>
</li>
<li>
    <span class="color-yellow">Title</span>
</li>
<li>
    <span class="color-purple">Title</span>
</li>
<li>
    <span class="color-red">Title</span>
</li>

有关最佳方法的任何建议吗?

1 个答案:

答案 0 :(得分:0)

AngularJS有一个名为ng-class的内置指令。您可以让范围返回要使用的元素的随机类,如下所示:

team_id_num.
<li ng-repeat="post in posts">
    <span ng-class="getRandomColorClass()">{{post.title}}</span>
</li>