我有一个 Handlebars 模板,我在其中尝试从数组生成以逗号分隔的项目列表。
在我的 Handlebars 模板中:
{{#each list}}
{{name}} {{status}},
{{/each}}
我希望 ,
不出现在最后一项上。有没有办法在 Handlebars 中执行此操作,还是我需要回退到 CSS 选择器?
更新:根据克里斯托弗的建议,这就是我最终实现的:
var attachments = Ember.CollectionView.extend({
content: [],
itemViewClass: Ember.View.extend({
templateName: 'attachments',
tagName: 'span',
isLastItem: function() {
return this.getPath('parentView.content.lastObject') == this.get('content');
}.property('parentView.content.lastObject').cacheable()
})
}));
在我看来:
{{collection attachments}}
和项目 View :
{{content.title}} ({{content.size}}) {{#unless isLastItem}}, {{/unless}}
最佳答案
我知道我迟到了,但我找到了 WAYYYY 更简单的方法
{{#unless @last}},{{/unless}}
关于javascript - 除了最后一个元素之后,如何在 {{#each}} 循环中的元素之间添加分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10403003/