Meteor Template不会在每个语句中输出数据

时间:2015-09-15 10:24:02

标签: javascript meteor

我有以下三个文件:

  • abc.js
  • abc.html
  • router.js

如果HTML被模板包围,HTML中的每个语句都不会输出数据。删除模板正在输出数据。

//file name : abc.js

if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: [
      { text: "This is task 1" },
      { text: "This is task 2" },
      { text: "This is task 3" }
    ]
  });
}
//file name router.js

Router.map(function () {
  this.route('mylist',{
    path:'/list',
  });
});
//file name: abc.html

<template name="mylist">
<body>
  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>
</template>

<template name="task">
  <li>{{text}}</li>
</template>

为什么模板不能用#each语句输出数据?

1 个答案:

答案 0 :(得分:1)

您正在使用Template.body.helpers而不是mylist助手 尝试修改您的代码,如下所示:

if (Meteor.isClient) {
  // This code only runs on the client
  Template.mylist.helpers({
    tasks: [
      { text: "This is task 1" },
      { text: "This is task 2" },
      { text: "This is task 3" }
    ]
  });
}

您还应该从<body>模板中删除mylist代码,并在您的html mylist上添加模板body

<body>
{{>mylist}}
</body>

<template name="mylist">

  <div class="container">
    <header>
      <h1>Todo List</h1>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>

</template>

<template name="task">
  <li>{{text}}</li>
</template>