在流星空间键模板中按索引/变量返回数组项

时间:2017-01-23 20:29:10

标签: meteor meteor-blaze

如何在流星模板中访问数组的X项?

感谢Return array item by index in a meteor spacebars template我知道如何为特定索引执行此操作:

<p>{{array.[0]}}</p>

但我的问题是如何为运行时定义的索引执行此操作。 假设X已定义且具有有效值。为什么这对我不起作用?

<p>{{array.[X]}}</p>

2 个答案:

答案 0 :(得分:2)

你可以尝试

<p>{{array.[index]}}</p> 例如。 <p>{{array.[0]}}</p>

{{#each getArray}}
    <div class="item" data-value="{{someHelper @index}}">
        {{this}}
    </div>
{{/each}}

答案 1 :(得分:0)

这里有同样的问题,

我最终使用了一般帮手, {{arrayIndex array index}}

简单

import { Template } from 'meteor/templating'

Template.registerHelper('arrayIndex', function (array, index) {
  return array[index]
})