How do I iterate over an array of Strings with the Meteor Spacebars {{#each}} block?

喜你入骨 提交于 2020-01-05 08:25:19

问题


I have a Mongo.Collection that holds 'Question' objects. Each 'Question' has a property called choices

Questions.insert({
    text: 'What is the capitol of the US?',
    choices: [
    "Washington D.C.",
    "Houston",
    "New York City",
    "Los Angeles"
    ],
    correctChoice: 0,
    date: new Date().toDateString()
});

In my template I have this:

<div class="question">
    <div class="question-content">
        <p>{{text}}</p>
        <ul>
            {{#each choices}}
            //?????????
            {{/each}}
        </ul>
    </div>
</div>

What should I put instead of the question marks in order for popular the unordered list with list items that contain the appropriate choices?

Thank you for reading. Sorry if this is easy. I'm still a little bit of a noob at Meteor. =)


回答1:


It looks like this was solved in a comment, but just so this question has an answer: you should use this in cases where the current context is a primitive.

<div class="question">
  <div class="question-content">
    <p>{{text}}</p>
    <ul>
      {{#each choices}}
        <li>{{this}}</li>
      {{/each}}
    </ul>
  </div>
</div>


来源:https://stackoverflow.com/questions/31282522/how-do-i-iterate-over-an-array-of-strings-with-the-meteor-spacebars-each-bl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!