Javascript ember cli支持每个助手中的把手@vars(即@index、@key、@first、@last)
每当我有一个Handelbars模板在每个助手中使用@vars变量(即@index、@key、@first、@last)时,我就会在ember cli中得到一个编译错误。(有关each helper中这些@vars变量的文档,请参阅。)下面是一个使用ember cli构建的简单应用程序,只包含添加到程序中的两个文件:routes/application.js和templates/application.hbs。在这篇文章的底部是由ember cli给出的编译错误消息的屏幕截图。我的代码中有错误吗?或者这是一个我应该在github上报告的bug@ routes/application.jsJavascript ember cli支持每个助手中的把手@vars(即@index、@key、@first、@last),javascript,ember.js,handlebars.js,ember-cli,Javascript,Ember.js,Handlebars.js,Ember Cli,每当我有一个Handelbars模板在每个助手中使用@vars变量(即@index、@key、@first、@last)时,我就会在ember cli中得到一个编译错误。(有关each helper中这些@vars变量的文档,请参阅。)下面是一个使用ember cli构建的简单应用程序,只包含添加到程序中的两个文件:routes/application.js和templates/application.hbs。在这篇文章的底部是由ember cli给出的编译错误消息的屏幕截图。我的代码中有错误吗
export default Ember.Route.extend({
model: function() {
return ['red', 'blue', 'green'];
}
});
模板/application.hbs
{{#each model}}
{{@index}}: {{this}}
{{/each}}
ember cli编译错误消息的屏幕截图:
以下是所涉及的各种工具的版本:
- 余烬cli:0.0.40
- 节点:0.10.30
- 净现值:1.4.21
- 车把:1.3.0
- 余烬:1.6.1
@index
,@key
,@first
,@last
@索引
@钥匙
@首先
您还可以使用插件模仿@first
的行为,并利用eq
helper-多亏了kristjan reinhold的这一想法:
{{#each array as |item index|}}
{{#if (eq index 0)}}
<!-- first element specific html -->
{{else}}
<!-- other html -->
{{/if}}
{{/each}}
谢谢,这对我来说是个新闻。您的评论帮助我找到了这篇相关的文章:您不是
{{if}
一个{{if}}
?另外,您可以使用(eq item array.lastObject)
实现@last
{{#each-in object as |key value|}}
{{key}}: {{value}}
{{/each-in}}
{{#each array as |item index|}}
{{#if (eq index 0)}}
<!-- first element specific html -->
{{else}}
<!-- other html -->
{{/if}}
{{/each}}
{{#each array as |item|}}
{{#if (eq item array.lastObject)}}
<!-- last element specific html -->
{{else}}
<!-- other html -->
{{/if}}
{{/each}}