问题
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