在流星中显示日期

时间:2016-01-14 23:34:12

标签: javascript html date templates meteor

我目前正在学习流星框架,现在我无法理解为什么我的代码无效。我正在尝试创建一个名为“time”的模板,该模板有一个名为“current_time”的变量,它使用新的Date();在我的HTML文件上显示时间,但它不起作用。你可以看到我的目标是显示一系列图像,然后显示时间,但所有显示的都是“现在是时间”而没有显示时间。

这是我的HTML和JS文件(我尝试使用与我课程使用的第一个模板图像相同的逻辑):

<head>
      <title>Welcome to My social media app... kind of.</title>
    </head>

<body>
  <h1>Hello from America!</h1>
  <div class="container">
  {{> images}}
  {{> time}}
 </div>

</body>

<template name="images">
    <div class="row">
   {{#each images}}
    <div class ="col-xs-12 col-md-3">
        <div class="thumbnail">
    <img class="js-image" src="{{img_src}}" alt="{{img_alt}}"/>

  <div class="caption">
     <h3>Thumbnail label</h3>
    <p>Description of the image</p>
 </div>
   </div>
     </div> <!-- /col -->

   {{/each}}
   </div> <!-- / row -->
   </template>

   <template name="time">
         <p>The time now is: {{current_time}}</p>
  </template>

    if (Meteor.isClient) {
       var img_data = [
      {
        img_src:"alley.JPG",
         img_alt:"Alley in town"
       },
      ];

      Template.images.helpers({images: img_data});

      Template.images.events({
        'click .js-image':function(event) {
          $(event.target).css("width", "50px");
       }
      });

      var current_time = new Date();

     Template.time.helpers({time: current_time});
    }

    if (Meteor.isServer) {
        // code to run on server at startup
       console.log("I am the server");
    }

1 个答案:

答案 0 :(得分:2)

将您的助手更改为:

Template.time.helpers({
  time: function(){ return new Date(); }
});
相关问题