Javascript 手柄js未在对象上迭代
我正在使用node.js构建一个应用程序,并在服务器端和客户端模板中使用手柄。但是,我在迭代对象时遇到问题。这是我的密码: My.js文件:Javascript 手柄js未在对象上迭代,javascript,node.js,handlebars.js,Javascript,Node.js,Handlebars.js,我正在使用node.js构建一个应用程序,并在服务器端和客户端模板中使用手柄。但是,我在迭代对象时遇到问题。这是我的密码: My.js文件: $(function () { var theTemplateScript = $("#update_address_distributor_template").html(); context.city = 'london'; var theTemplate = Handlebars.compile(theTemplateScript);
$(function () {
var theTemplateScript = $("#update_address_distributor_template").html();
context.city = 'london';
var theTemplate = Handlebars.compile(theTemplateScript);
var theCompiledHtml = theTemplate(context);
$('.update_address_distributor_template_placeholder').html(theCompiledHtml);
});
我的html文件:
<script id="update_address_distributor_template" type="text/x-handlebars-template">
<form class="form-horizontal" role="form" id="update_distributor_form">
\{{city}} //correctly prints out london
\{{address_distributors.length}} //returns 2, so i know the object exists
{{#each address_distributors}}
\{{name}} //does not print anything
{{/each}}
</form>
</script>
我的{{{每个../address}。。。{{/each}
不打印任何内容。我知道我的addres\u distributors
对象存在,因为\{{address\u distributors.length}
在html页面上打印2。另外,为了测试我的把手模板是否正确设置,我设置了context.city='london'
查看它是否会打印出来,确实如此。所以我知道这个问题和物体有关
有人能帮忙吗
提前谢谢 变量
name
不存在。试一试
{{this.name}}
另外,你也可以试试看
{{#each address_distributors}}
Anything // Just to see if your loop works.
{{/each}}
您的数据有问题,我尝试了以下数据:
{ "address_distributors": [ { name: 'nike', id: '1' }, { name: 'yokohama', id: '4' } ], city: 'london' }
我得到以下结果:
london //correctly prints out london
2 //returns 2, so i know the object exists
nike //does not print anything
yokohama //does not print anything
这是我测试的一把小提琴:这个
。/
有点奇怪不?我把它拿了出来;我仍然不工作..名字在哪里/如何定义?嗨,“任何东西”都不起作用。我用一个示例上下文对象更新了我的问题,所以你的循环甚至不起作用。为什么您首先要使用。/
?
london //correctly prints out london
2 //returns 2, so i know the object exists
nike //does not print anything
yokohama //does not print anything