2017-08-28 66 views
1

我想用each循环解释。但我没有能够得到一个好的解决方案(烬方式!)如何解释每个循环?

如何处理这个scernario?

从循环我打印16数字。每当index达到4 (index % 4 == 0)我想添加一个连字符。( - )

如何实现这个?

这里是我的route.js:

import Ember from 'ember'; 

    export default Ember.Route.extend({ 
     model(){ 
     return [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; 
     } 
    }); 

我HBS文件:

<h1>Each with condition </h1> 
{{#each model as |num index |}} 
    {{index}} 
{{/each}} 

,但我看起来像:

<h1>Each with condition </h1> 
{{#each model as |num index |}} 
    {{index}} {{ if index % 4 == 0 }} -- {{/if}} 
{{/each}} 

那么,什么是正确的做法灰烬这个?

Twiddle here

回答

3

你需要编写自己的助手来实现这一目标。我更新your twiddle

帮手/我-helper.js

import Ember from 'ember'; 
export function myHelper([index,position,totalLength]) { 
    if(index === totalLength-1){ 
    return ''; 
    } 
    index = index+1; 
    return index%position === 0 ? " -- ":""; 
} 
export default Ember.Helper.helper(myHelper); 
在application.hbs

{{#each model as |num index |}} 
    {{index}}{{my-helper index 4 model.length}} 
{{/each}} 
+0

我玩弄没有更新。你能看看吗? – 3gwebtrain

+0

我现在更新了我的回答 – kumkanillam

+0

了。非常感谢你。但仍然是第16位数字不需要后的最后一次炒作,如何删除? – 3gwebtrain