把手js不迭代对象

我正在使用node.js构build应用程序,并使用服务器端和客户端模板的句柄。 不过,我在迭代对象时遇到了问题。 这是我的代码:

我的.js文件:

$(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_distributors: { [ { name: 'nike', id: '1' }, { name: 'yokohama', id: '4' } ] }, city: 'london' } 

我的{{#each ../address_distributors}} ... {{/each}}不打印任何内容。 我知道我的addres_distributors对象退出,因为\\{{address_distributors.length}}在html页面上打印2。 另外,为了testing我的把手模板是否正确设置,我设置了context.city = 'london'; 看看是否会打印出来 所以我知道这个问题与对象有关…

有人可以帮忙吗?

提前致谢!

数据有问题,我试了下面的数据:

 { "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 

这里是我的testing小提琴: https : //jsfiddle.net/ChristopheThiry/30xh7epu/

namevariables不存在。 尝试

 {{this.name}} 

另外,你可以试试

  {{#each address_distributors}} Anything // Just to see if your loop works. {{/each}}