在Angular 2的for ... of指令中获取索引 [英] Get index in a for...of directive in Angular 2
本文介绍了在Angular 2的for ... of指令中获取索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,是否有一种方法可以在Angular 2的for..of
指令中获取可迭代的当前索引?换句话说,相当于Angular.js v1中的$index
...
As of now, is there a way to get the current index of the iterable in a for..of
directive in Angular 2? In others words, the equivalent of $index
in Angular.js v1...
代码示例:
<ul *for="#task of allTasks">
<li>{{ $index}} - {{ task.label }}</li>
</ul>
(当然,此代码不起作用,它不提供当前索引)
(of course this code does not work, it does not provide the current index)
推荐答案
<ul>
<template ngFor let-task [ngForOf]="allTasks" let-i="index">
<li>{{ i }} - {{ task.label }}</li>
</template>
</ul>
但是您必须使用最新版本的快速入门
But you have to use the very most recent version of the quickstart
顺便说一句-以上等同于以下语法糖
BTW - the above is the equivalent of the following syntax sugar
<li *ngFor="let task of allTasks; let i=index">{{ i }} - {{ task.label }}</li>
这篇关于在Angular 2的for ... of指令中获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文