Javascript 下划线.js:1461未捕获引用错误:未定义用户
我不熟悉backbone.js和underline.js。我正在尝试创建一个示例应用程序 我的代码是:Javascript 下划线.js:1461未捕获引用错误:未定义用户,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,我不熟悉backbone.js和underline.js。我正在尝试创建一个示例应用程序 我的代码是: <script type="text/template" id="user-list-template"> <table class = "table striped"> <thead> <tr> <th>User Name<
<script type="text/template" id="user-list-template">
<table class = "table striped">
<thead>
<tr>
<th>User Name</th>
<th>Age</th>
<th>Location</th>
<th></th>
</tr>
</thead>
<tbody>
<% _.each(users, function(user){%>
<tr>
<td><% user.get('sUserName') %></td>
<td><% user.get('iAge') %></td>
<td><% user.get('sLocation') %></td>
<td></td>
</tr>
<% }) %>
</tbody>
</table>
</script>
但我有一个例外:
未捕获引用错误:未定义用户
在htmldevelment.eval(在m.template(下划线.js:1454),:6:9处求值)
在htmldevelment.c(下划线.js:1461)
at n.access(jquery.min.js:3)
位于n.fn.init.html(jquery.min.js:3)
成功时((索引):64)
at Object.t.success(backbone.js:1051)
在j(jquery.min.js:2)
在Object.fireWith[as resolveWith](jquery.min.js:2)
在x(jquery.min.js:4)
在XMLHttpRequest.b(jquery.min.js:4)
如前所述,您应该使用
(或者更好的是
进行转义),但您的主要问题似乎是调用模板的方式
下划线中的.\template()
函数依次返回一个可重用函数,您可以使用不同的数据调用该函数
var users=新主干网。集合([
{sUserName:'爱丽丝',iAge:35,sLocation:'伦敦'},
{sUserName:'鲍勃',iAge:5,sLocation:'布宜诺斯艾利斯'}
]);
var template=35;.template($('#用户列表模板').html());
$('#result').html(模板({users:users.models}))代码>
用户名
年龄
位置
如前所述,您应该使用
(或者更好的是
进行转义),但您的主要问题似乎是调用模板的方式
下划线中的.\template()
函数依次返回一个可重用函数,您可以使用不同的数据调用该函数
var users=新主干网。集合([
{sUserName:'爱丽丝',iAge:35,sLocation:'伦敦'},
{sUserName:'鲍勃',iAge:5,sLocation:'布宜诺斯艾利斯'}
]);
var template=35;.template($('#用户列表模板').html());
$('#result').html(模板({users:users.models}))代码>
用户名
年龄
位置
在中添加“=”像这样
并更改那样。$el.html(模板)
tothat.$el.html(模板).toJSON()代码>查看哪个解释了它的作用和使用方法。@EmileBergeron他们可能正在使用一个带有较新下划线的旧教程。在1.7版本之前,您可以使用\模板(tmpl,data)
,但在1.7版本之后,您必须使用两步流程。有一些旧的教程仍然在谷歌搜索结果的顶部。@muistooshort是的,很有可能。在这个问题上有很多问题,这就是为什么我没有回答这个问题的原因
tothat.$el.html(模板).toJSON()代码>查看哪个解释了它的作用和使用方法。@EmileBergeron他们可能正在使用一个带有较新下划线的旧教程。在1.7版本之前,您可以使用\模板(tmpl,data)
,但在1.7版本之后,您必须使用两步流程。有一些旧的教程仍然在谷歌搜索结果的顶部。@muistooshort是的,很有可能。关于这个问题有很多问题,所以我没有回答。谢谢。它的工作,并帮助我很多。。。。。再次感谢,谢谢。它的工作,并帮助我很多。。。。。再次感谢。