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

问题描述 投票:0回答:2

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

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

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

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

<p>{{array.[X]}}</p>
meteor meteor-blaze
2个回答
2
投票

你可以试试

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

要么

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

0
投票

同样的问题,

我结束使用一般助手,{{arrayIndex array index}}

很简单

import { Template } from 'meteor/templating'

Template.registerHelper('arrayIndex', function (array, index) {
  return array[index]
})
© www.soinside.com 2019 - 2024. All rights reserved.