嵌套在jQuery模板中 [英] Nested eachs in jQuery template

查看:159
本文介绍了嵌套在jQuery模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取嵌套{{each}}上的父$ value?我希望使用直接"解决方案,而不必创建第二个模板,并从第一个模板开始使用它.

How can I get the parent $value on a nested {{each}}? I would prefer a "direct" solution, without having to create a 2nd template, and use it from the first one.

推荐答案

您可以执行以下操作:${ parentItem.Name },如果父项称为parentItem,而所需的成员变量称为Name.这是一个示例,大致基于针对{{each}}的jQuery API文档中的电影示例:

You can just do this: ${ parentItem.Name }, if the parent item was called parentItem and the desired member variable was called Name. Here is an example, based loosely on the movie example in the jQuery API docs for {{each}}:

模板:

<li>
    Title: ${Name}.<br />
    {{each(i, edition) Editions}}
        ${i + 1}: <em>${edition.Name}. </em><br />
        Languages: <br />
        {{each(i, language) Languages}}
            ${language} (${$data.Name}, ${edition.Name}) <br />  
        {{/each}}
        <br />
    {{/each}}
</li>

数据(此示例中只有一项):

var movies = [
    {
        Name: "City Hunter",
        Editions: [
            {
                Name: "Original", 
                Languages: ["Mandarin", "Cantonese"]
            },
            {
                Name: "DVD",
                Languages: ["French", "Spanish"]
            }
        ]
    }
];

结果:


Title: City Hunter.
1: Original.
Languages:
Mandarin (City Hunter, Original)
Cantonese (City Hunter, Original)

2: DVD.
Languages:
French (City Hunter, DVD)
Spanish (City Hunter, DVD) 

在此示例中,内部{{each}}从以下位置获取值:

In this example, the inner {{each}} is getting values from:

  • 带有${edition.Name}的外部{{each}}
  • 具有${$data.Name}
  • 的根模板数据
  • The outer {{each}} with ${edition.Name}
  • The root template data with ${$data.Name}

这篇关于嵌套在jQuery模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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