我正在使用流星鲨鱼分支。
有没有办法访问空格键中每个块助手内的数组索引?
我正在寻找这样的东西。
{{#each humans}}
{{this.arrayIndex}}
{{/each}}
我正在使用流星鲨鱼分支。
有没有办法访问空格键中每个块助手内的数组索引?
我正在寻找这样的东西。
{{#each humans}}
{{this.arrayIndex}}
{{/each}}
空格键在 1.2 中获得了很多功能,包括原生的@index
. 不再需要助手来解决这个问题——你可以简单地这样做:
<template name="showHumans">
<ul>
{{#each humans}}
<li>{{@index}}: {{name}}</li>
{{/each}}
</ul>
</template>
我在“动画”一章的流星书中看到了一个使用模板助手的类似示例。您可以将 amap
应用于人类光标以添加如下索引:
Template.showHumans.helpers({
humans: function() {
return Humans.find({}, {sort: {hotness: -1}}).map(function(human, index) {
human.rank = index;
return human;
});
}
});
<template name="showHumans">
<ul>
{{#each humans}}
<li>{{rank}}: {{name}}</li>
{{/each}}
</ul>
</template>
取自空格键文档:
您可以在 #each 的主体中使用特殊变量 @index 来获取序列中当前呈现值的从 0 开始的索引。
在 Meteor 1.0.2.1 中,您可以执行以下操作:
{{#each humans}}
{{this}}
{{/each}}
这是因为 #each 遍历数组,使每个循环中的 this 简单地等于当前值。