【问题标题】:Each is not working as supposed with Handlebar每个都不能像 Handlebar 那样工作
【发布时间】:2015-09-30 15:05:12
【问题描述】:

在使用 Handlebar 4.0.3 的应用程序中,我有这个简单的模板:

{{#each certificates}}
  <tr>
    <td data-title='Certification'>{{this}}</td>
  </tr>
{{/each}}

我使用这样编译它:

result = template(certificates: ['test'])

我有这个结果:

<table class="table table-hover table-condensed tablesorter">
  <tbody><tr>
    <td data-title="Certification">[object Object]</td>
  </tr></tbody>
</table>

我认为我应该使用“test”而不是“[object Object]”。

我试过这个模板:

{{#each certificates}}
<tr>
  <td data-title='Certification'>{{this}}</td>
  <td data-title='Certification'>{{name}}</td>
</tr>
{{/each}}

还有这个 JavaScript :

result = template({ certificates: [{name: 'Name'}]})

我有 tyhis 的结果:

<table class="table table-hover table-condensed tablesorter">
  <tbody><tr>
    <td data-title="Certification">[object Object]</td>
    <td data-title="Certification"></td>
  </tr></tbody>
</table>

如您所见,{{name}} 没有给出任何内容而不是“名称”。

我是不是做错了什么?

编辑 1

我发现了问题。

我有这个 html :

  <div id='modal-site'>
    <table>
      <tbody>
        {{#each site.certificates}}
        <tr>
          <td>{{this.name}}</td>
        </tr>
        {{/each}}
      </tbody>
    </table>
  </div>
</div>

当我这样做 $('#modal-site').html() 时,我得到了这个结果:

{{#each site.certificates}}

{{/each}}
<table>
  <tbody><tr>
    <td>{{this.name}}</td>
  </tr></tbody>
</table>

each 在外面,所以车把不能工作。你有解决方案的想法吗?

【问题讨论】:

标签: javascript ember.js handlebars.js


【解决方案1】:

对于模板的第二个实现,如果你想访问字符串数据,你应该在 HTML 中使用{{this.name}}

【讨论】:

  • 制作整个解决方案的 jsfiddle。我在您的脚本中看不到 site 对象,其中包含 certificates 数组。
【解决方案2】:

使用以下语法:

{{#each certificates as |cert|}}
<tr>
  <td data-title='Certification'>{{cert}}</td>
</tr>
{{/each}}

【讨论】:

    猜你喜欢
    • 2015-05-16
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多