我正在使用 Meteor 的 Blaze 作为 html 模板,并且我有多个循环,例如:
let objects = [{name: 'John', age: 18}, {name: 'Foo', age: 25}, {name: 'Bar', age: 35}]
let attrs = ['name', 'age']
{{#each objects}}
<h3>Object {{@index}}</h3>
{{#each attrs}}
[...] // code here
{{/each}}
{{/each}}
我知道:
{{@index}}
用于了解当前循环索引([..]
{{@index}}
是对 attrs 数组索引的引用{{this}}
用于了解当前循环值([...]
{{value}}
是姓名
或年龄
){{..}}
是父循环值的引用([...]
{{..}}
code> 是当前对象,在第一个循环中)
现在,我想要 [...]
上 objects
循环的当前 index。我在 Google 和 Stackoverflow 上搜索了很多但没有找到。
最佳答案
Humm,关于 Blaze 的新闻允许 new 创建变量(使用 #let)。一个简单的解决方案是:
let objects = [{name: 'John', age: 18}, {name: 'Foo', age: 25}, {name: 'Bar', age: 35}]
let attrs = ['name', 'age']
{{#each objects}}
{{#let object=this object_idx=@index}}
<h3>Object {{object_idx}}</h3>
{{#each attrs}}
Object idx {{object_idx}} | Object {{object}}
Attr idx {{@index}} | Attr {{this}}
{{/each}}
{{/let}}
{{/each}}
更新:你甚至可以做 {{#each object in objects}}
一段时间后,避免你做 {{#let object=this }}
{{#each object in objects}}
{{#let object_idx=@index}}
<h3>Object {{object_idx}}</h3>
{{#each attr in attrs}}
Object idx {{object_idx}} | Object {{object}}
Attr idx {{@index}} | Attr {{attr}}
{{/each}}
{{/let}}
{{/each}}
关于javascript - Meteor Blaze 上的多个每个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35089843/