我试图从把手中的辅助函数返回一个值并显示它 这是我的代码 -
<div id="test"></div>
<script id="template" type="text/x-handlebars-template">
{{#each this}}
<hr/>
<div id="table">
<table>
<tr>
<td>
<div id="steps" style="color: #000000; font-size:36px;">{{steps}}</div><br></td>
</td>
</tr>
</table>
</div>
{{/each}}
</script>
<script
src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.0/mustache.min.js"></script>
<script type="text/javascript"
src="//ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<script>
$(document)
.ready(
function() {
var source = $("#template").html();
var template = Handlebars.compile(source);
var rdata = [ {
"totalSteps" : 100,
"name" : "Dummy",
"heightFeet" : 5,
"averageSteps" : 10,
}, {
"totalSteps" : 10000,
"name" : "TestMan",
"heightFeet" : 4,
"averageSteps" : 10,
} ];
Handlebars.registerHelper('steps', function(rdata,index) {
var val=rdata[index].totalSteps;
return val.toLocaleString();
});
var ht = template(rdata);
$("#test").html(ht);
});
</script>
但是,我收到此错误
Uncaught TypeError: Cannot read property 'totalSteps' of undefined
另外,我试着写这样的函数 -
$("#class").each function(index) {
return rdata[index].totalSteps;
});
但它没有显示任何内容。 任何人都可以帮我解决这个问题。
答案 0 :(得分:0)
您需要将所需数据传递给帮助程序。这是一个简化版本。
{{#each rdata as |value index|}}
{{steps ../rdata index}}
{{/each}}
{
rdata: [ {
totalSteps: 100,
name: "Dummy",
heightFeet: 5,
averageSteps : 10,
}, {
totalSteps : 10000,
name: "TestMan",
heightFeet: 4,
averageSteps: 10,
}]
}
Handlebars.registerHelper('steps', function(rdata,index) {
var val=rdata[index].totalSteps;
return val.toLocaleString();
});
以上示例适用于https://docs.gradle.org/current/userguide/build_environment.html
答案 1 :(得分:0)
我刚刚做了这个
Handlebars.registerHelper('steps', function() {
return this.totalSteps;
});