ngFor,其中索引作为属性中的值 [英] ngFor with index as value in attribute
问题描述
我有一个简单的ngFor
循环,该循环也跟踪当前的index
.我想将该index
值存储在属性中,以便可以打印它.但是我不知道这是如何工作的.
I have a simple ngFor
loop which also keeps track of the current index
. I want to store that index
value in an attribute so I can print it. But I can't figure out how this works.
我基本上有这个:
<ul *ngFor="#item of items; #i = index" data-index="#i">
<li>{{item}}</li>
</ul>
我想将#i
的值存储在属性data-index
中.我尝试了几种方法,但没有一个起作用.
I want to store the value of #i
in the attribute data-index
. I tried several methods but none of them worked.
我在这里有一个演示: http://plnkr.co/edit/EXpOKAEIFlI9QwuRcZqp?p=预览
I have a demo here: http://plnkr.co/edit/EXpOKAEIFlI9QwuRcZqp?p=preview
如何在data-index
属性中存储index
值?
推荐答案
我将使用以下语法将索引值设置为HTML元素的属性:
I would use this syntax to set the index value into an attribute of the HTML element:
您必须使用let
声明值,而不是#
.
You have to use let
to declare the value rather than #
.
<ul>
<li *ngFor="let item of items; let i = index" [attr.data-index]="i">
{{item}}
</li>
</ul>
角度= 1
<ul>
<li *ngFor="#item of items; #i = index" [attr.data-index]="i">
{{item}}
</li>
</ul>
以下是更新的插件: http://plnkr.co/edit/LiCeyKGUapS5JKkRWnUJ?p=预览.
这篇关于ngFor,其中索引作为属性中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!