如何在Meteor JS中将数组值加载到模板变量?

Ven*_*kat 2 javascript meteor

如何在Meteor中将数组值加载到模板变量?请看下面的代码并建议我该怎么做?

HTML代码:

 <template name="header">
 <div class="header">
    {{#each alphabets}}
       <div class="alphabets">{{this}}</div>
    {{/each}}
 </div>
</template>
Run Code Online (Sandbox Code Playgroud)

JS代码:

 //the below array values are load dynamically above template
 var Alphas = ['ALL',
              'A', 'B', 'C', 'D','E', 'F',
              'G', 'H', 'I', 'J','K', 'L',
              'M', 'N', 'O', 'P','Q', 'R',
              'S', 'T', 'U', 'V','W', 'X',
              'Y', 'Z'
              ]

        Template.header.alphabets = function (i)
         {
           return Alphas[i];
         };
Run Code Online (Sandbox Code Playgroud)

Ser*_*soy 10

模板html:

<template name="header">
  <div class="header">
    {{#each alphabets}}
      <div class="alphabets">{{this}}</div>
    {{/each}}
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)

模板js:

var Alphas = ['ALL',
              'A', 'B', 'C', 'D','E', 'F',
              'G', 'H', 'I', 'J','K', 'L',
              'M', 'N', 'O', 'P','Q', 'R',
              'S', 'T', 'U', 'V','W', 'X',
              'Y', 'Z'];

Template.header.alphabets = function() {
  return Alphas;
};
Run Code Online (Sandbox Code Playgroud)

我测试了它,它的工作原理.

基本上,你可以像游标一样传递数组,每个都会迭代它们.

如果您的数组中有键/值对,您也可以像mongo文档一样处理它们.