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

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...