我有一个如下所示的each循环,我想在第一个元素前添加'(',在最后一个元素后添加')'。有人可以帮我怎么做吗?任何帮助都将非常非常有帮助-谢谢
{{#each v.NOVNoticeTypeName as |vv iindex|}}
{{get v.NOVNoticeTypeName iindex}}: {{get v.NOVNumber iindex}}
<br/>
{{/each}}
答案 0 :(得分:1)
使用ember-truth-helpers只是检查元素是数组中的第一个元素还是最后一个(不确定要使用(和)做什么,所以我只是猜测一下:
{{#each v.NOVNoticeTypeName as |vv iindex|}}
{{#if (or (eq v.NOVNoticeTypeName.firstObject vv) (eq v.NOVNoticeTypeName.lastObject vv))}}
({{get v.NOVNoticeTypeName iindex}}: {{get v.NOVNumber iindex}})
{{else}}
{{get v.NOVNoticeTypeName iindex}}: {{get v.NOVNumber iindex}}
{{/if}}
<br/>
{{/each}}
答案 1 :(得分:1)
我建议结合使用ember-truth-helpers和ember-composable-helpers的has-next
和has-previous
。
{{#each v.NOVNoticeTypeName as |vv iindex|}}
{{#if (not (has-previous vv v.NOVNoticeTypeName))}}
{{!-- This is the first element in the array}} --}}
({{get v.NOVNoticeTypeName iindex}}: {{get v.NOVNumber iindex}}
{{else if (not (has-next vv v.NOVNoticeTypeName))}}
{{!-- This is the last element in the array}} --}}
{{get v.NOVNoticeTypeName iindex}}: {{get v.NOVNumber iindex}})
{{else}}
{{!-- This is everything except the first and last element --}}
{{get v.NOVNoticeTypeName iindex}}: {{get v.NOVNumber iindex}}
{{/if}}
<br />
{{/each}}