简体   繁体   English

如何在角度2、4、5中与* ngFor一起使用索引

[英]how to used index with *ngFor in angular 2, 4, 5

if you are using angular 2, 4/5** you can't used $index directly, for using index you need to define first 如果您使用的是角2、4 / 5 **,则不能直接使用$ index,因为要使用index,您需要先定义

<li *ngFor="let item of items; let i = index">{{item}} - {{i}}</li>

{{i}} is the index of items {{i}}是项目的索引

but in angular 1.x you can used index like this: 但是在angular 1.x中,您可以使用如下所示的索引:

<li ng-repeat="let item of items>{{item}} - {{$index}}</li>

if you are using angular 2+ then you should do like this for getting index , so in angular 2 onwards you don't have ng-repeat it replaced by *ngFor 如果您使用的是angular 2+,则应该这样做以获得index,因此从angular 2开始,您不需要ng-repeat替换为*ngFor

<li *ngFor="let user of userObservable | async as users; 
                         index as i; ">
   {{i}}/{{users.length}}. {{user}}
</li>

or 要么

<li *ngFor="let user of userObservable | async as users; 
                         let i = index; ">
   {{i}}/{{users.length}}. {{user}}
</li>

From the doc : NgForOf 从文档: NgForOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM