【问题标题】:Meteor Blaze display arrayMeteor Blaze 显示阵列
【发布时间】:2016-12-07 20:08:44
【问题描述】:

我有这样的收藏:

例如,我想遍历 object.questions.teema。

我有帮手:

Template.game.helpers({
    theGame: function() {
        var theGame = Game.findOne({_id:"LhQmaZKW8eJNenyX9"})
        console.log(theGame)
        return theGame
    }
});

和模板:

<template name="game">

{{#with theGame}}
  {{#each theGame.questions}}
    {{teema}}
  {{/each}}
{{/with}}
</template>

但是不行,模板有什么问题?

【问题讨论】:

    标签: meteor handlebars.js meteor-blaze


    【解决方案1】:

    theGame.questions 是具有 teema 键的对象数组的数组(您可以迭代)。因此,您仍然需要遍历 2 级数组,或在该数组中定义特定项,然后才能最终使用 teema 属性访问对象。

    可能是这样的:

    {{#with theGame}}
      {{#each questions}}
        {{#each this}}
          {{this.teema}}
        {{/each}}
      {{/each}}
    {{/with}}
    

    但这取决于你为什么首先拥有这些 2 级数组。

    【讨论】:

    • 所以是这样的:{{#with theGame}} {{#each theGame.questions}} {{#each questions.teema}} {{teema}} {{/each}} { {/each}} {{/with}} 这似乎也不起作用
    • 不。使用暂定代码编辑答案。首先检查为什么你需要那些 2 级数组...
    • 这是个好建议,我不需要 2 级数组。谢谢。
    【解决方案2】:

    {{teema}} 应该是什么?

    无论如何,正如您从 console.log 语句中看到的那样,{{theGame.questions}} 返回另一个数组。但是该数组返回对象。这真的很难用 Blaze 查询。

    更好的解决方案是将其展平,使您的数据形状如下:

    questions: [
       {
           a: 'asdfkjah',
           level: 'askdjfhal',
           q: 'asdkfh',
           teema: 'asdkfjh'
           vaartus: 100
       },
       {
          ...
       }
    ]
    

    这样你就没有一个数组嵌套在数组中。这将允许您:

    {{#with theGame}}
      {{#each theGame.questions}}
        {{this.teema}}
      {{/each}}
    {{/with}}
    

    【讨论】:

      【解决方案3】:

      '#each theGame.questions' 在#with 中不起作用,因为您可以直接访问'theGame' 对象。

      关键是当您尝试在#with 中获取Game 对象时,它会返回未定义的,因为“theGame”对象没有您想在#with 块中访问的Game 属性。

      <template name="game">
        {{#with theGame}}
          {{#each questions}}
             //Thie #each because you have nested array. As I can see in your console log.
             {{#each this}}
               {{teema}}
             {{/each}}
          {{/each}}
        {{/with}}
      </template>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-09
        • 2014-05-27
        • 2017-08-16
        • 2016-12-24
        • 1970-01-01
        • 2015-02-14
        • 2021-07-28
        相关资源
        最近更新 更多