【问题标题】:options.data has multiple index elements in handlebars helperoptions.data 在车把助手中有多个索引元素
【发布时间】:2013-07-31 21:44:33
【问题描述】:

我有一个结构如下的车把模板:

{{#each array}}
    some stuff
    {{#each array2}}
        some more stuff
        {{#customHelper}}{{/customHelper}}
    {{/each}}
{{/each}}

每个数组包含四个项目。

车把助手看起来像这样:

Handlebars.registerHelper('customHelper', function(options){
    console.log(options.data);
});

options.data 包含通过 #each 的次数 (index)。如果有多个#each,则返回最里面的一个。但是,我想要最外层的索引#each。

记录options.data 给出:

Object {index: 0, index: 0}
Object {index: 1, index: 0}
Object {index: 2, index: 0}
Object {index: 3, index: 0}
Object {index: 0, index: 1}
Object {index: 1, index: 1}
Object {index: 2, index: 1}
Object {index: 3, index: 1}
Object {index: 0, index: 2}
Object {index: 1, index: 2}
Object {index: 2, index: 2}
Object {index: 3, index: 2}
Object {index: 0, index: 3}
Object {index: 1, index: 3}
Object {index: 2, index: 3}
Object {index: 3, index: 3}

这表明两个索引都存在。第一个索引是最里面的#each,第二个索引是最外面的。据我所知,在一个 javascript 对象中甚至不可能有两个相同的键。

记录options.data.index 给出第一个index,而不是第二个。

是否可以访问第二个index,如果可以,如何访问?

【问题讨论】:

    标签: javascript handlebars.js


    【解决方案1】:

    我想通了。 删除索引:

    delete options.data.index;
    

    将只删除第一个,所以现在

    options.data.index
    

    将返回外部#each的索引

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-05
      • 2013-02-05
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多