2017-03-03 52 views
0

問題可能很難理解,但我的問題很簡單:在Angular2中使用ngFor,我該如何做一個「inner」ngFor?

  • 我想顯示的聯繫人列表。
  • 我使用的引導顯示它們爲卡
  • 我想每行顯示4張牌

HTML結構:

<div class="row" *ngFor="let contact of contacts"> 
    <!-- Here I want to loop 4 of the contacts before rendering a new row --> 
    <div class="col-md-3"> 
     <contact-card [contact]="contact"></contact-card> 
    </div> 
    </div> 

或者更簡單的,我想這樣做實現這一目標: enter image description here

有沒有辦法做一個「內部」ngFor在col-md-3 div,它會在渲染4個聯繫人後停止?

+0

聽起來像http://stackoverflow.com/questions/42557781/angular-2-show-hide-wrap-tag-in-ngfor-over-even-odd-items/42558062#42558062 –

+0

你可以添加'let i = index'到你的'* ngFor',然後你就可以在'i'變量上獲得當前卡的索引。 –

+0

你想要所有4個聯繫人連續相同或不同? –

回答

0

您可以創建兩個組件:一個顯示單張卡片,另一個顯示一排卡片。

整體代碼更多,但每個組件都更簡單,更具內聚性和可重用性;這兩個組件是分離的。

最後,您可以將所有卡片的數據作爲數據流處理,並形成所需數量的卡片以批量填充行。