Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 如何获得流星中的索引_Meteor - Fatal编程技术网

Meteor 如何获得流星中的索引

Meteor 如何获得流星中的索引,meteor,Meteor,如何获取meteor中的每个循环索引。@索引不起作用。请帮助我 Template.apichange.helpers({ api_rest_data: function () { return Session.get("api_rest_list"); } }); {{#each api_rest_data}} <tr> <

如何获取meteor中的每个循环索引。@索引不起作用。请帮助我

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}} 
Template.apichange.helpers({
api_rest_数据:函数()
{     
return Session.get(“api_rest_list”);
}
});
{{{#每个api_rest_data}
{{@index}}
{{/每个}}

它需要另一个助手,请查看我在《流星》一书中使用的:

Template.registerHelper('withIndex', function (list) {
    var withIndex = _.map(list, function (v, i) {
        v.index = i;
        return v;
    });
    return withIndex;
});
这将使用索引注册名为
的全局帮助程序。每当您在
每个
上下文中使用的数组上调用它时,它将允许您使用
{{index}}
,就像您使用
{@index}}
告诉每个元素在数组中的位置一样

调整您的包含标签,首先将api_rest_数据传递到带有索引的

{{#each withIndex api_rest_data}}

非常感谢Stephan提供了这一中间解决方案。这是我的版本;它允许变量
{{index}}
&
{{value}}
{{each}}
上下文中

Template.registerHelper('withIndex',函数(数组){
返回映射(数组,函数(val,i){
返回{
"索引":i,,
“值”:val
};
});
});

顺便说一句,
{@index}}
根据把手/空格键规范的上下文应该很快在新的Meteor版本中发布,根据。

我认为这可能是
UI.registerHelper
而不是
Template.registerHelper
。另一方面,我认为这是一个优雅的解决方案。
UI
名称空间在年被弃用。现在是正确的函数。看起来它已合并。