Create a for loop that will iterate a certain number of times in Ember-CLI

天大地大妈咪最大 提交于 2019-11-29 10:20:07
Kalman

According to the docs, bound helpers do not support blocks - see here

You can create a increment-for component as follows. Create a component that expects a times property. Then, you can have a numOfTimes property that returns an array with length times. Finally, you can use a combination of #each and yield helpers to display your content.

Component code:

import Ember from 'ember';

export default Ember.Component.extend({
  numOfTimes: Ember.computed('times', function() {
    const times = parseInt(this.get('times'));
    return new Array(times);
  })
});

Component template:

{{#each numOfTimes as |time|}}
  {{ yield }}
{{/each}}

Component usage:

{{#increment-for times=2 }}
  <div>How goes it?</div>
{{/increment-for}}

Working solution here

As a complement to the answer of @Kalman and you would use the ember-composable-helper addon, it's even simpler.

By using the repeat helper, a loop is easily created:

{{#each (repeat 3) as |empty|}}
  I will be rendered 3 times
{{/each}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!