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

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

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

  1. {{#each-in myArray.[0] as |key value|}}
  2. <th>
  3. {{value}}
  4. </th>
  5. {{/each-in}}
  6. {{#each-in myArray.[1] as |key value|}}
  7. <th>
  8. {{value}}
  9. </th>
  10. {{/each-in}}

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

  1. {{#let index=0}}
  2. {{#each-in myArray.[index] as |key value|}}
  3. <th>
  4. {{value}}
  5. </th>
  6. {{/each-in}}
  7. {{/let}}


我用的是ember2.13.

kmb7vmvb

kmb7vmvb1#

你很接近了!

  1. {{#let 0 as |index|}}
  2. {{#each-in (get myArray index) as |key value|}}
  3. <th>
  4. {{value}}
  5. </th>
  6. {{/each-in}}
  7. {{/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}}无法正常工作。
在任何人都可能需要的情况下发布它:

  1. <tr>
  2. {{#each-in myArray as |index object|}}
  3. <tr>
  4. {{#each-in (get myArray index) as |key value|}}
  5. <th>
  6. {{value}}
  7. </th>
  8. {{/each-in}}
  9. </tr>
  10. {{/each-in}}
  11. </tr>

字符串

相关问题