使用 ng-repeat 迭代数组块 [英] iterate over chunks of an array using ng-repeat

查看:29
本文介绍了使用 ng-repeat 迭代数组块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器从服务器抓取人员:

My controller grabs people from the server:

$scope.people = [person1, person2, person3, person4, person5,...]

我的模板需要每行显示三个人.例如,如果它是一张桌子:

My template needs to display three people per line. For example if it was a table:

<table>
    <tr>
        <td>person1</td><td>person2</td><td>person3</td>
    </tr>
    <tr>
        <td>person4</td><td>person5</td><td>person6</td>
    </tr>
</table>

我不确定如何在 $index % 3 == 0 时有条件地应用 或最佳实践是什么.我知道如何通过向控制器添加分组逻辑来实现这一点,但我认为最好将设计逻辑保留在控制器之外.

I wasn't sure how to conditionally apply the <tr> when $index % 3 == 0 or what the best practices are. I know how to do this by adding grouping logic to the controller but I thought it would be best to keep design logic out of the controller.

推荐答案

经过多年的 Angular 经验,很明显,最好的方法是在控制器中将数组拆分成块.每次修改原始数组时,更新分块数组.

After years of angular experience, It's obvious that the best way to do this is to split the array into chunks in the controller. Each time the original array is modified, update the chunked array.

这篇关于使用 ng-repeat 迭代数组块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆