[英]Angular2: How to know number of iterators on *ngFor
我想知道ngFor上迭代器的數量,以便基於此我可以顯示或隱藏該元素,例如,我想這樣做,以便如果達到迭代器數量,它應該停止迭代。 到目前為止,整個模板是:
<p *ngIf = "heros2.length > 3">
there are two many users
</p>
<div *ngIf = "heros2.length == 3">
<ul>
<li *ngFor = 'let hero1 of heros2'>
there are many users
{{hero1.name}}
</li>
</ul>
</div>
要獲得ngFor的索引,您需要使用*ngFor="let element of elements; let i=index"
,其中“ i”是當前索引。 停止ngFor的一種方法可能是使用python策略創建一個數組,該數組具有要迭代的元素的確切數量:
<div *ngFor="let element of range(6); let i=index">
{{ elements[i].name }}
</div>
范圍函數返回具有6個位置的數組。 這樣,您可以遍歷已定義計數中的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.