访问每个循环内容的另一个变量 [英] Accessing another variable with contents of each loop

查看:69
本文介绍了访问每个循环内容的另一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传递到车把模板的数据如下:

The data being passed into the handlebars template looks like this:

a = {
    x: {
       name: "xavier"
    },
    y: {
       name: "yamal"
    },
}

b = {
    properties: {
        x: {
            property: "number"
        }
    }
}

车把模板如下:

<div class="left-panel">
    {{a.x.name}} // Prints correctly "xavier"
    {{#each b.properties}}
        <h4>{{@key}}</h4> // Prints correctly "x"
        <h4>{{ ../a.[@key].name }}</h4> // does not print "xavier"
    {{/each}}
</div>

如您所见,我希望能够使用 b 中的键来访问 a 中字典的 name .我该如何实现?

As you can see, I want to be able to access the name of a dict in a using the key in b. How can I achieve this?

推荐答案

您的问题与此基本相同: Handlebars.js-使用变量键访问对象值

Your question is essentially the same as this one: Handlebars.js - Access object value with a variable key

唯一的额外细节是,您需要按顺序使用车把子表达式进行两次查找(首先是 @key ;然后是'name').

The only extra detail is that you will need to make use of Handlebars subexpressions in order to perform the two lookups (first of @key; then of 'name').

子表达式将允许您在 ../a 上查找 @key 的值,并将其传递给'name值的第二次查找'有关第一次查找的结果.

A subexpression will allow you to lookup the value of @key on ../a and pass that to a second lookup of the value of 'name' on the result of the first lookup.

模板中的相关行变为:

<h4>{{lookup (lookup ../a @key) 'name'}}</h4>

这里有个小提琴供您参考: https://jsfiddle.net/76484/b0puy52n/

Here is a fiddle for your reference: https://jsfiddle.net/76484/b0puy52n/

这篇关于访问每个循环内容的另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆