2013-10-17 55 views
11

我想遍历一个枚举,并显示一个计数器获取镖聚合物重复模板指数

<div template repeat="{{ name in names }}"> 
    ###. {{name}} 
</div> 

我应该怎么放,而不是###因此它显示名称的位置:

1. John Doe 
2. Jane Doe 
3. etc... 

回答

17

聚合物现在有能力做到这一点:取自

<template repeat="{{fruit in fruits | enumerate}}"> 
    <li>You should try the {{fruit.value}}. It is number {{fruit.index}}.</li> 
</template> 

https://github.com/sethladd/dart-polymer-dart-examples/blob/master/web/iterate_loop_index/my_example.html

+0

这不再适用于聚合物0.5。改为使用Wolfram的解决方案。 我知道这是2年前的答案,但让我注意到从搜索到达的人。 –

+0

这仍然是聚合物0.5的方式。也许你的意思是1.0? – pjv

2

它在web_ui中可用,但尚未在Polymer中提供。

Web UI是polymer.dart的前身。 Polymer.dart几乎与Web UI的功能相同。下面是尚未在polymer.dart实现了Web UI功能列表:

指数变量的值可循环

https://www.dartlang.org/polymer-dart/#web-ui-parity

在此之前的里面,你可以使用列表上的asMap并遍历keys

<template repeat="{{i in names.asMap().keys)}}"> 
    <div>{{i}}: {{names[i]}}</div> 
    </template> 
+1

你甚至不必重写该名称:在遍历集合德兴。 {{index in names.asMap.keys}} –

+0

@AlanKnight非常酷,谢谢你的提示。我更新了我的答案以使用您的建议。 – Nathanial

7

其他解决方案不适用于聚合物0.3.4(不再?),但有documentation on templates,包括在内

<template repeat="{{ foo, i in foos }}"> 
    <template repeat="{{ value, j in foo }}"> 
    {{ i }}:{{ j }}. {{ value }} 
    </template> 
</template>