each
你可以使用 each helper代码遍历列表。在块内,你可以使用 this 来引用被迭代的元素。
参数
list{Array}- The list to iterate over
用法
{{#each list}}
{{this}}
{{/each}}
例子
{{#each collection.products as |product|}}
{{ product.title }}
{{/each}}
else
您可以选择提供一个 else 部分,该部分仅在列表为空时显示。
{{#each collection.products as |product|}}
{{ product.title }}
{{else}}
No products
{{/each}}
@index
当遍历 each 中的项目时,你可以选择通过 {{@index}} 引用当前循环的索引。
{{#each collection.products as |product|}}
{{ @index }} - {{ product.title }}
{{/each}}
@first / @last
在数组上进行迭代时,通过 @first 和 @last 变量记录迭代的第一项和最后一项。
{{#each collection.products as |product|}}
{{#if @first}}
{{ product.title }}
{{/if}}
{{#if @last}}
{{ product.title }}
{{/if}}
{{/each}}
这篇文章对你有帮助吗?
SHOPLINE