在余烬把手中使用每个

时间:2019-11-07 23:51:51

标签: javascript ember.js ember-cli

我有一个如下所示的each循环,我想在第一个元素前添加'(',在最后一个元素后添加')'。有人可以帮我怎么做吗?任何帮助都将非常非常有帮助-谢谢

{{#each  v.NOVNoticeTypeName as |vv iindex|}}
    {{get v.NOVNoticeTypeName iindex}}: {{get v.NOVNumber iindex}}
    <br/>                                         
{{/each}}

2 个答案:

答案 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-helpersember-composable-helpershas-nexthas-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}}