【发布时间】:2018-05-03 21:32:15
【问题描述】:
这是我的html:
<table id="tableUserPaymentTypeRight">
<col width="50%"/>
<col width="25%"/>
<col width="25%"/>
<script id="rowUserPaymentTypeRight" type="text/x-handlebars-template">
{{#each Data}}
<tr>
<td>{{FullName}}</td>
<td>
{{#if Right_Add}}
<input type="checkbox" value="{{UserID}}_0" checked/>
{{else}}
<input type="checkbox" value="{{UserID}}_0"/>
</td>
<td>
{{#if Right_Confirm}}
<input type="checkbox" value="{{UserID}}_1" checked/>
{{else}}
<input type="checkbox" value="{{UserID}}_1"/>
</td>
</tr>
{{/each}}
</script>
这是我的 js:
var userPaymentTypeRight = $('#rowUserPaymentTypeRight').html();
var userPaymentTypeRightTpl = Handlebars.compile(userPaymentTypeRight);
var context = data;
var html = userPaymentTypeRightTpl(context);
$('#tableUserPaymentTypeRight').html(html);
这样的数据:
{
{UserID: 26031, FullName: "Aaron Zubler", Right_Add: null, Right_Confirm:null, RowIndex: 1},
{UserID: 26390, FullName: "Achilleas Hoppas", Right_Add: null, Right_Confirm: null, RowIndex: 2},
{UserID: 26168, FullName: "Ai Ke", Right_Add: null, Right_Confirm: null, RowIndex: 3},
{UserID: 26310, FullName: "Alessandra Giordanella", Right_Add: null, Right_Confirm: null, RowIndex: 4}
}
根据 API 似乎是正确的,但我得到了错误: 未捕获的 TypeError:Handlebars.compile 不是函数。
【问题讨论】:
-
Handlebars 定义了吗? Handlebars.compile 怎么样?我有和你完全相同的代码,但我使用 requirejs 来加载它。也许您正在查看的示例代码也通过诸如 requirejs 之类的模块加载器加载它?我会先自己研究一下包含/加载/脚本包含机制。
-
我也是用requirejs来加载的,
-
你的定义是什么样的?这是我的 define(['bootstrap','jquery',"handlebars"], function(bootstrap, $, Handlebars){。firefox/chrome 开发工具 Net 选项卡是否显示 GET车把有用吗?
-
是的,它定义正确,并且车把的获取请求有效。
-
我认为您应该发布您在代码中使用的 requirejs 定义。您对 API 本身的使用看起来与我的相同。
标签: handlebars.js