Meteor blaze按数组索引选择特定项

时间:2014-10-15 18:19:21

标签: meteor meteor-blaze

有没有办法在流星大火中的每个块助手中访问数组索引?

我正在寻找类似的东西。

{{#each myarray}}
    {{this.arrayIndex3}}
{{/each}}

1 个答案:

答案 0 :(得分:1)

我担心还没有一种标准的方法可以做到这一点,但是你可以写一个帮助器,将你的数组映射到一个索引/值对列表并迭代它以显示你想要的东西。

JS

Template.myTemplate.helpers({
  myArrayWithIndex: function(){
    return _.map(this.myArray,function(value,index){
      return {
        index:index,
        value:value
      };
    });
  }
});

HTML

<template name="myTemplate">
  {{#each myArrayWithIndex}}
    myArray[{{index}}] == {{value}}
  {{/each}}
</template>

您还可以定义一个名为{{#eachWithIndex}}的块助手,它将自动执行此过程。