2013-10-28 43 views
16

訪問對象值尋找一種方式來獲得實現這一點:Handlebars.js - 一個可變密鑰

{{#each someArray}} 
    {{../otherObject.[this]}} 
{{/each}} 

如何評估的this值,然後引用它作爲重點,以我的對象otherObject ?與助手

+0

閱讀側邊欄中的相關問題並進行一些搜索,我確定這是一個重複的問題。 –

+4

我有,搜索了2天。也許我只是在使用谷歌搜索。 –

+1

[This side from the sidebar](http://stackoverflow.com/questions/18168924/how-to-get-an-array-value-at-index-using-handlebars-js?rq=1)約爲90解決方案的百分比。基本上,您必須編寫自己的幫助器,將諸如'{{value_at obj key}}'之類的東西轉換爲JavaScript'obj [key]'。 –

回答

5

一個可能的解決方案:

/* 
{{#each someArrayOfKeys}} 
    {{#withItem ../otherObject key=this}} 
    {{this}} 
    {{/withItem}} 
{{/each}} 
*/ 

Handlebars.registerHelper('withItem', function(object, options) { 
    return options.fn(object[options.hash.key]); 
}); 
0

正如其他答案已經指出的那樣,你可能想lookup

但是,如果您使用EmberJS(在撰寫本文時爲2.12.0),則需要get