我跟着http://mozmonkey.com/2014/03/ember-getting-the-index-in-each-loops/博客文章通过帮助程序添加了索引计数器。单个每个循环都可以很好地工作。但是当我使用嵌套的每个循环时,值会重复。示例:
{{#each item in data}}
{{#eachIndexed record in item.innerdata}}
{{index_1}}
{{/eachIndexed}}
{{/each}}
我在数据中有两个对象,每个内部数据中有两个对象。预期结果是
1 2 3 4
但我正在
1 2 1 2
我如何得到预期的结果?我的车把版本是1.1.2,而ember版本是1.6.1。
答案 0 :(得分:3)
如果您的数据是:
[
{innerdata: ['foo', 'bar']},
{innerdata: ['foo', 'bar']}
]
然后它按预期工作,因为它是一个索引而不是一个计数器。
{{#each item in data}}
Outer Index: {{index_1}}<br>
{{#eachIndexed record in item.innerdata}}
Inner Index: {{index_1}}<br>
{{/eachIndexed}}
{{/each}}
输出:
外指数:1
内部指数:1
内部指数:2
外部指数:2
内部指数:1
内部指数:2
如果您想计算迭代次数,可以尝试这个答案:https://stackoverflow.com/a/15376389/29347
(function() {
var positionCounter = 1;
Handlebars.registerHelper('position', function() {
return positionCounter++;
});
})();
这给了你:
{{#each item in data}}
{{#eachIndexed record in item.innerdata}}
{{position}}
{{/eachIndexed}}
{{/each}}
答案 1 :(得分:0)
嵌套每个块可以通过基于深度访问交互变量 路径。例如,要访问父索引,{{@ .. / index}}可以 使用