【发布时间】:2018-06-07 13:10:09
【问题描述】:
这是我传递给车把模板的模型
module.exports = function(showHeader, userId){ // the data model
return {
userId: userId,
seminars: userSeminars;
};
};
var userSeminars = [{ // some information
seminarId: 1,
isRated: true
}, {
seminarId: 2,
isRated: false
}];
在渲染我的模板时,我使用这个 HTML 代码
{{#each seminars}}
<div class="seminarContainer">
{{#if isRated}}
<button onclick="loadStatistics({{seminarId}}, {{userId}})">Do Something 1</button>
{{else}}
<button onclick="loadQuestionnaire({{seminarId}})">Do Something 2</button>
{{/if}}
</div>
{{/each}}
但在调试时,{{userId}} 未定义。我去了这个测试例程并将它写在上面每个循环
$(document).ready(function () {
alert({{userId}}); // this works
});
userId 不是未定义的。但是当迭代数组时它是未定义的。如何将数组范围留在循环中?我必须访问数据模型对象,而不是数组中的属性。
编辑
Access a variable outside the scope of a Handlebars.js each loop
当我想离开范围时,我可以使用{{../userId}}
循环内
<p>{{../userId}}</p>
工作正常,但将其用于
等参数时onclick="loadStatistics({{seminarId}}, {{../userId}})"
第二个参数未定义。
【问题讨论】:
标签: html node.js express handlebars.js