微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 为什么嵌套模板输出中的每一个都没有

虽然这是按预期呈现的:

{{#each Items}}  // a collection
  {{title}}  
{{/each}}  
{{#each types}}  // another ollection
  {{refId}}  
{{/each}}  

如果我把它放在一起:

{{#each Items}}  
  {{title}}  
  {{#each types}}  
    {{refId}}  
  {{/each}} 
{{/each}} 

#each类型为空.

模板助手:

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}}
  

即使用{{#each ../restMenuSideItems}}替换{{#each restMenuSideItems}},也不会出现任何问题.

怎么了?

最佳答案
因为#each子句将数据上下文更改为当前项.

如果您想要每个Items中的类型列表,您可以使用…获取父数据上下文

如果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.
  }
});

原文地址:https://www.jb51.cc/js/428966.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐