在meteor 中,有没有办法访问空格键中的数组索引 [英] In meteor is there a way to access array index in spacebars

查看:17
本文介绍了在meteor 中,有没有办法访问空格键中的数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是meteor Shark分支.

I am using meteor Shark branch.

有没有办法访问空格键中每个块助手内的数组索引?

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

我正在寻找这样的东西.

I am looking for something like this.

{{#each humans}}
  {{this.arrayIndex}}
{{/each}}

推荐答案

meteor >= 1.2

空格键在 1.2 中获得了很多功能,包括原生的 @index.不再需要助手来解决这个问题——你可以简单地这样做:

meteor >= 1.2

Spacebars gained a lot of functionality in 1.2, including a native @index. Helpers are no longer needed to solve this problem - you can simply do this:

<template name="showHumans">
  <ul>
    {{#each humans}}
      <li>{{@index}}: {{name}}</li>
    {{/each}}
  </ul>
</template>

流星<1.2

我在 meteor book 的动画"一章中看到了一个使用模板助手的类似示例.您可以将 map 应用于人类光标以添加如下索引:

meteor < 1.2

I saw a similar example using template helpers in the meteor book in the "animations" chapter. You can apply a map to the humans cursor in order to add an index like so:

Template.showHumans.helpers({
  humans: function() {
    return Humans.find({}, {sort: {hotness: -1}}).map(function(human, index) {
      human.rank = index;
      return human;
    });
  }
});

<template name="showHumans">
  <ul>
    {{#each humans}}
      <li>{{rank}}: {{name}}</li>
    {{/each}}
  </ul>
</template>

这篇关于在meteor 中,有没有办法访问空格键中的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆