Meteor blaze select a specific item by the array index [duplicate]

一曲冷凌霜 提交于 2019-12-23 10:53:04

问题


Is there a way to access array index inside each block helper in meteor blaze?

I am looking for something like this.

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

回答1:


I'm afraid there is not yet a standard way to do this, however you can write a helper that maps your array to a list of index / value pairs and iterate over it to display what you want.

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>

You could also define your own block helper called {{#eachWithIndex}} that would automate this process.



来源:https://stackoverflow.com/questions/26389414/meteor-blaze-select-a-specific-item-by-the-array-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!