使用ember.js在hbs模板中设置变量

0qx6xfy6  于 2023-08-04  发布在  其他
关注(0)|答案(2)|浏览(156)

我有一个数据数组,我需要在我的hbs模板中迭代它:

{{#each-in myArray.[0] as |key value|}}
  <th>
    {{value}}
  </th>
{{/each-in}}

{{#each-in myArray.[1] as |key value|}}
  <th>
    {{value}}
  </th>
{{/each-in}}

字符串
如何声明一个变量并增加它来替换硬编码的值?
我尝试使用{{#let}}进行 Package ,但没有成功:

{{#let index=0}}
  {{#each-in myArray.[index] as |key value|}}
    <th>
      {{value}}
    </th>
  {{/each-in}}
{{/let}}


我用的是ember2.13.

kmb7vmvb

kmb7vmvb1#

你很接近了!

{{#let 0 as |index|}}
  {{#each-in (get myArray index) as |key value|}}
    <th>
      {{value}}
    </th>
  {{/each-in}}
{{/let}}

字符串
关于let的文档:https://api.emberjs.com/ember/5.0/classes/Ember.Templates.helpers/methods/let?anchor=let
关键是模板使用不同的语法,这里描述了https://cheatsheet.glimmer.nullvoxpopuli.com/docs/templates
这里是working demo
(Note这使用了新的gjs格式,教程在这里:https://tutorial.glimdown.com

wr98u20j

wr98u20j2#

这终于对我起作用了,多亏了NullVoxPopuli的回答让我走上了正确的道路。由于某些原因,{{#let}}无法正常工作。
在任何人都可能需要的情况下发布它:

<tr>
  {{#each-in myArray as |index object|}}
  <tr>
    {{#each-in (get myArray index) as |key value|}}
    <th>
      {{value}}
    </th>
    {{/each-in}}
  </tr>
  {{/each-in}}
</tr>

字符串

相关问题