在Angular JS中同时迭代两个数组

时间:2014-07-04 14:30:49

标签: html angularjs iteration

我有两个数组,我想在一个数组中显示它们 使用ng-repeat

的会话方式

假设有两个数组像这样定义

messages_send = ["hi","good, How about you?","cool","LOL","NFW"]   
messages_received = ["hey, How are you?","good, Thanks","hmmm","asdfsda"]
<ul>
    <span ng-repeat="i in [0,1,2,3,4]">
        <li>
            messages_send[i]
        </li>
        <li>
            messages_received[i]
        </li>
    </span>
</ul>

我是这样做的,只是问题是它弄乱了我的对话布局。还有另外一种方法吗?我的意思是使用ng-reapet迭代两个数组而不会弄乱布局?

由于

1 个答案:

答案 0 :(得分:1)

您可以对此进行一些修改,但是您需要对数组进行错误检查以确保它具有相同数量的元素。我只是使用ng-show进行错误检查,但是这样的话:

<ul>
    <span ng-repeat="msg in messages_send track by $index>
        <li>
            {{msg}}
        </li>
        <li> 
          <span ng-show='messages_received.length>=$index'>
            {{messages_received[$index]}}
          </span>
        </li>
    </span>
</ul>