gpt4 book ai didi

angular - 在 Angular 2 的 for...of 指令中获取索引

转载 作者:太空狗 更新时间:2023-10-29 16:50:44 24 4
gpt4 key购买 nike

到目前为止,有没有办法在 Angular 2 的 for..of 指令中获取可迭代项的当前索引?换句话说,相当于 Angular.js v1 中的 $index...

代码示例:

<ul *for="#task of allTasks">
<li>{{ $index}} - {{ task.label }}</li>
</ul>

(当然这段代码不行,它不提供当前索引)

最佳答案

<ul>
<template ngFor let-task [ngForOf]="allTasks" let-i="index">
<li>{{ i }} - {{ task.label }}</li>
</template>
</ul>

但是你必须使用最新版本的快速入门

顺便说一句 - 上面相当于下面的语法糖

<li *ngFor="let task of allTasks; let i=index">{{ i }} - {{ task.label }}</li>

关于angular - 在 Angular 2 的 for...of 指令中获取索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29335285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com