2014-07-04 61 views
2

我有兩個陣列,我想使用一個 對話的方式顯示它們NG-重複迭代通過兩個陣列在角JS同時

採取兩個陣列這樣

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迭代兩個數組而不會搞亂佈局?

感謝

+0

爲什麼不將它們映射到一個數組? – charlietfl

回答

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>