如何在嵌套{{each}}
上获取父$ $?我更喜欢"直接"解决方案,无需创建第二个模板,并从第一个模板中使用它。
答案 0 :(得分:5)
您可以这样做:${ parentItem.Name }
,如果父项被调用parentItem
并且所需的成员变量被称为Name
。这是一个例子,基于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}}
从以下位置获取值:
{{each}}
与${edition.Name}
${$data.Name}