如何在每个流星中获取索引 [英] How to get the index in meteor in each
问题描述
如何获取meteor中的每个循环索引.@index不起作用.请帮帮我.
How to get the each loop index in meteor.@index is not working.Please help me.
Template.apichange.helpers({
api_rest_data: function ()
{
return Session.get("api_rest_list");
}
});
{{#each api_rest_data}}
<tr>
<td><select id="methodname"> <option id="optn" value="{{ method_name }}"> {{ @index }} </option></select></td>
</tr>
{{/each}}
推荐答案
它需要另一个助手,查看 我在我的 Meteor 书中使用的解决方案:
It requires another helper, check out my solution that I have used in my book on Meteor:
Template.registerHelper('withIndex', function (list) {
var withIndex = _.map(list, function (v, i) {
v.index = i;
return v;
});
return withIndex;
});
这会注册一个名为 withIndex
的全局助手.每当您在 each
上下文中使用的数组上调用它时,它将允许您以与使用 相同的方式使用
告诉每个元素在数组中的哪个位置.{{index}}
{{@index}}
This registers a global helper named withIndex
. Whenever you call it on an array that is used inside the each
context it will allow you to use {{index}}
in the same way you would have used {{@index}}
to tell which position in the array each element has.
调整您的包含标签以首先将 api_rest_data
传递给 withIndex
:
Adjust your inclusion tag to pass api_rest_data
to withIndex
first:
{{#each withIndex api_rest_data}}
这篇关于如何在每个流星中获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!