我使用Node.js,express和Handlebars在客户端渲染模板。我没有收到任何错误,但模板没有填充。
view:detail.hbs
<table id="attachmentTable" class="table table-condensed box-bottom-space">
<tbody>
...
</tbody>
</table>
// The back slash before the curly brackets are required, without it errors out.
<script id="rowTemplate" type="text/x-handlebars-template">
\{{#each this}}
<tr>
<td class="attachments">
<i class="fa fa-file-text"></i>
<a target="_blank" href="/uploads/\{{this}}">\{{this}}</a>
<a class="deleteAttachmentIcon text-danger" href="javascript:;" data-deletefile="\{{this}}"><span class="fa fa-remove"></span></a>
</td>
</tr>
\{{/each}}
</script>
<script type="text/javascript" src="/js/handlebars-v4.0.5.min.js"></script>
<script>
$(function($) {
$.ajax({
url: '/processes/upload/{{processes._id}}',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response){
if(response.success){
console.log(response.file);
var rTemplate = $("#rowTemplate").html();
var crTemplate = Handlebars.compile(rTemplate);
$("#attachmentTable body").append(crTemplate(response.file));
}
}
});
});
</script>
这是来自服务器的响应。
{"success":true,"msg":"The file(s) has been Upladed Successfully!","file":["June Updates1-1519927100102.docx","export-11-03-2017-1519927100103.xls"]}
我错过了什么?谢谢你的帮助!
答案 0 :(得分:1)
不要太看其他代码:
$("#attachmentTable body").append(crTemplate(response.file));
您可能需要在此处选择tbody
,而不是body
。