简体   繁体   中英

Angular ngFor access index

How can I access index inside the ngFor?

https://stackblitz.com/edit/angular-ekze7q

Thanks

You forgot to add a * in ngFor and the var name is wrong

<p *ngFor="let i of Arr(count); let index = index">
  <button (click)="removeElement(index)">test</button>
</p>

If you want to print the numbering using index try this, it will help you.

<ul *ngFor="let item of Array; let i = index">
 <li> {{i+1}}={{item.name}}</li> 
</ul>

For reference: https://angular.io/api/common/NgForOf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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