[英]ember-cli support for Handlebars @vars in each helper (i.e., @index, @key, @first, @last)
每當我有一個使用@vars變量(即@ index,@ key,@ first,@ pop)的Handelbars模板時,我就會在ember-cli中收到編譯錯誤。 (有關每個幫助程序中這些@vars變量的文檔,請參閱http://handlebarsjs.com/#iteration 。)下面是一個使用ember-cli構建的簡單應用程序,只包含兩個添加到程序中的文件:routes / application.js和templates / application.hbs。 在這篇文章的底部是ember-cli給出的編譯錯誤消息的屏幕截圖。 我的代碼中有錯誤嗎? 或者這是我應該在github上報告的錯誤@ https://github.com/stefanpenner/ember-cli ?
路線/ application.js中
export default Ember.Route.extend({
model: function() {
return ['red', 'blue', 'green'];
}
});
模板/ application.hbs
{{#each model}}
{{@index}}: {{this}}
{{/each}}
ember-cli編譯錯誤消息的屏幕截圖:
以下是涉及的各種工具的版本:
這真的與ember-cli無關。 Ember Handlebars不支持@keyword項目。
可以模仿以下Handlebars關鍵字的行為: @index
, @key
, @first
, @last
。
{{#each array as |item index|}}
Index of item: `{{item}}` is: `{{index}}`
{{/each}}
{{#each-in object as |key value|}}
{{key}}: {{value}}
{{/each-in}}
你也可以使用ember-truth-helpers addon模仿@first
行為並利用eq
helper - 感謝kristjan reinhold的這個想法:
{{#each array as |item index|}}
{{#if (eq index 0)}}
<!-- first element specific html -->
{{else}}
<!-- other html -->
{{/if}}
{{/each}}
而不是(eq index 0)
您可以使用(eq item array.firstObject)
。
正如dwickern建議您可以使用Ember.Array.lastObject
來模仿@last行為。
{{#each array as |item|}}
{{#if (eq item array.lastObject)}}
<!-- last element specific html -->
{{else}}
<!-- other html -->
{{/if}}
{{/each}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.