Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用';通过handlebar.js访问嵌套数据;这';作为第二个属性值_Javascript_Json_Nested_Handlebars.js - Fatal编程技术网

Javascript 使用';通过handlebar.js访问嵌套数据;这';作为第二个属性值

Javascript 使用';通过handlebar.js访问嵌套数据;这';作为第二个属性值,javascript,json,nested,handlebars.js,Javascript,Json,Nested,Handlebars.js,我无法通过手柄访问嵌套数据。我搜索过类似的问题,但没有一个涉及使用“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

我无法通过手柄访问嵌套数据。我搜索过类似的问题,但没有一个涉及使用“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}}
引发一个未捕获的错误:无效路径

是否有方法使用“this”的值访问嵌套数据

我对完全不同的解决方案持开放态度,但我真的很想知道如何使用当前模型来实现这一点。

创建帮助程序:

Handlebars.registerHelper('getProperty', function(context, key) {
    return context[key];
});
用法示例:

{{getProperty ../img_alt this}}

例如,我希望{../img_alt.this}}表达式通过activity_组在img_alt.foo、img_alt.bar和img_alt.blargh分别获取每个循环的值。所以首先是“foo-alt”,然后是“bar-alt”,然后是“blargh-alt”。与使用的其他表达式的意图相同。首先,我要确保../var返回正确的值,然后尝试../var[this]的括号表示法,然后尝试(..var/)[this]可能。../var返回正确的值,但不幸的是,您的建议都不起作用。两者的分析错误。
{{getProperty ../img_alt this}}