2017-08-28 71 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