如何解释每个循环?
How to interpret with each loop?
我正在尝试用 each
循环进行解释。但我无法提供好的解决方案(ember 方式!)
如何处理这种情况?
从循环中我正在打印 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}}
那么,正确的 ember 方法是什么?
您需要编写自己的帮助程序来实现此目的。我更新了 your twiddle
helpers/my-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}}
我正在尝试用 each
循环进行解释。但我无法提供好的解决方案(ember 方式!)
如何处理这种情况?
从循环中我正在打印 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}}
那么,正确的 ember 方法是什么?
您需要编写自己的帮助程序来实现此目的。我更新了 your twiddle
helpers/my-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}}