将变量从#each循环传递给helper emberjs

时间:2014-09-08 06:56:10

标签: javascript loops ember.js handlebars.js each

我在将模板变量传递给帮手时遇到问题,我得到了一个像这样的模板

{{#each row in workpage.mainStructure}} <div class='row'> <div class='cell cell-2'>{{i18nForecasts row.value }}</div> {{#each cell in row.amounts}} <div class='cell'>{{cell.format}}</div> {{/each}} </div> {{/each}}

我的助手只需要记录传递的val:

 Ember.Handlebars.registerHelper('i18nForecasts', function(property, options) {   
   var escaped = Handlebars.Utils.escapeExpression(property);
  console.log(escaped);
});

问题是,我只能得到“&#39; row&#39;在字符串中而不是循环值。但是,如果我尝试用帮助器打印值,如:

{{row.value}}

打印正确的循环值。

1 个答案:

答案 0 :(得分:2)

您希望使用registerBoundHelperhttp://emberjs.com/api/classes/Ember.Handlebars.html#method_registerBoundHelper

Ember.Handlebars.registerBoundHelper('i18nForecasts', function(property, options) {   
   var escaped = Handlebars.Utils.escapeExpression(property);
   console.log(escaped);
});