为什么嵌套在模板中的每个内容都不输出任何内容
Why does nested each in template output nothing
虽然这是按预期呈现的:
{{#each Items}} // a collection
{{title}}
{{/each}}
{{#each types}} // another ollection
{{refId}}
{{/each}}
如果我把它放在一起:
{{#each Items}}
{{title}}
{{#each types}}
{{refId}}
{{/each}}
{{/each}}
#each types
为空。
模板帮助程序:
Template.menuItems.helpers({
restMenuItems: function() {
return RestMenuItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()});
},
restMenuSideItems: function() {
return RestMenuSideItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()});
}
});
Template.menuItems.onCreated( function(){
this.subscribe('restMenuItems');
this.subscribe('restMenuSideItems');
});
还有一些模板代码:
{{#each restMenuItems}}
<div>
<select class="list-group select_side_addons">
{{#each restMenuSideItems}}
<option>{{restRefId}}</option>
{{/each}}
</select>
</div>
{{/each}}
即使用{{#each ../restMenuSideItems}}
替换{{#each restMenuSideItems}}
,也没有任何显示。
怎么了?
因为 #each
子句将数据上下文更改为当前项。
如果你想要每个Items
内types
的列表,你可以使用 ..
获取父数据上下文。
如果types
是父上下文的属性:
{{#each Items}}
{{title}}
{{#each ../types}}
{{refId}}
{{/each}}
{{/each}}
如果types
是模板帮助程序,则可以将父上下文作为参数传递给它:
{{#each Items}}
{{title}}
{{#each types ..}}
{{refId}}
{{/each}}
{{/each}}
,然后使用查询的上下文。
Template.myTemplate.helpers({
types: function(parent) {
// you now have the parent context here.
// use parent._id etc. where you used this._id in the
// "flat" version.
}
});
相关文章:
- jQuery中是否内置了任何字符串格式化函数
- 有人知道有没有jquery插件可以在图像上写文本,并让用户将其放置在图像内的任何位置
- 是否有任何内置方法可以更改JavaScript对象'的属性设置为某个值
- AngularJS-检测到n分钟内没有任何操作并进入默认状态
- 使用Jade输出HTML(在ng个重复内)
- 使用多功能框在 chrome 扩展程序中包含内联自动填充功能的任何方法
- 创建按钮不调用表单内的任何操作
- 如何在角度过滤器内返回 html5 音频/视频作为输出
- 使用 CasperJS 时,是否可以在执行任何内联或外部 Javascript 之前与加载页面的 DOM 进行交互
- 循环内输出奇怪
- 找不到“”;应用程序“;模板或视图.对象{fullName:“template:application”}将不呈现任何内
- 我的for循环内输出分叉失败
- 如何在没有任何内置函数的情况下将元素添加到数组中
- 如何获得元素id点击没有任何内联函数调用在javascript
- 在javascript/jquery中是否有任何内置功能来过滤邮件,如php's函数(!使用filter_var
- 在JavaScript中过滤数组,而不使用任何内置函数或新数组
- Jquery是否有任何内置方法来单独查找可验证的元素?
- 我试图在不使用任何内置方法的情况下延迟此函数.为什么这行不通呢?
- 如何在不使用javascript中任何内置函数的情况下反转字符串
- Angular Bootstrap-ui 是否有任何内置函数来处理打开或关闭的东西