0
これは私が私のハンドルバーに渡すモデルテンプレート急行・ハンドルでオブジェクトを反復
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
は未定義ではありませんザ・各ループをを書きます。しかし、配列を反復処理するときは未定義です。どのようにループ内に配列スコープを残すことができますか?配列の属性ではなく、データモデルオブジェクトにアクセスする必要があります。
EDIT
Access a variable outside the scope of a Handlebars.js each loop
私は
<p>{{../userId}}</p>
{{../userId}}
を使用することができますスコープを残したいとき
が正常に動作しますが、
onclick="loadStatistics({{seminarId}}, {{../userId}})"
のようなパラメータのためにこれを使用したときに、第2のパラメータが定義されていません。