使用ng-repeat遍历数组的大块 [英] iterate over chunks of an array using ng-repeat

查看:132
本文介绍了使用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>

我不知道如何有条件地应用< tr& / code>当 $ 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.

推荐答案

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

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天全站免登陆