我无法通过把手访问嵌套数据。我搜索过类似的问题,但没有一个涉及使用'this'的值作为属性名称。
以下是数据:
var activity_groups = ["foo", "bar", "blargh"]; //this array is generated dynamically and may have any or all of these elements
var hb_data = {
"activity_groups" : activity_groups,
"img_alt" : {
"foo" : "foo alt",
"bar" : "bar alt",
"blargh" : "blargh alt"
},
"img_src" : {
"foo" : "foo src",
"bar" : "bar src",
"blargh" : "blargh src"
},
"title" : {
"foo" : "foo title",
"bar" : "bar title",
"blargh" : "blargh title"
},
"desc" : {
"foo" : "foo desc",
"bar" : "bar desc",
"blargh" : "blargh desc"
}
}
模板:
{{#each activity_groups}}
<div data-type="{{this}}">
<img class="activity_group_selector_img" alt="{{../img_alt.this}}" src="{{../img_src.this}}" />
<span>{{../title.this}}</span>
<div class="activity_group_selector_description">{{../desc.this}}</div>
</div>
{{/each}}
问题在于:
{{../img_alt.this}}
{{../img_src.this}}
{{../title.this}}
{{../desc.this}}
抛出未捕获错误:路径无效
有没有办法使用'this'的值来访问嵌套数据?
我对完全不同的解决方案持开放态度,但我真的很想知道如何使用当前的模型。
答案 0 :(得分:0)
创建帮助器:
Handlebars.registerHelper('getProperty', function(context, key) {
return context[key];
});
使用示例:
{{getProperty ../img_alt this}}