我想显示当前日期。我编写了以下代码,但它没有显示value
变量的值。
<head>
<title>my_first_app</title>
</head>
<body>
<h1>Hello from Pakistan</h1>
{{> hello}}
{{> date}}
</body>
<template name="hello">
<button>Click Me</button>
<p>You've pressed the button {{counter}} times.</p>
</template>
<template name="date">
<p>The time now is {{value}}.</p>
</template>
JS:
if (Meteor.isClient) {
// counter starts at 0
Session.setDefault('counter', 0);
Template.hello.helpers({
counter: function () {
return Session.get('counter');
}
});
Template.hello.events({
'click button': function () {
// increment the counter when button is clicked
Session.set('counter', Session.get('counter') + 1);
}
});
var value = new Date();
Template.date.helpers(value);
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
答案 0 :(得分:1)
template.date.helpers需要一个对象,每个可返回结果都有方法。正如您在hello模板上使用计数器帮助程序一样:
Template.date.helpers({
value: function () {
return new Date();
}
});
答案 1 :(得分:1)
如果您想显示当前时间,可以写下面的内容
Template.date.helpers({
value: function () {
return Session.get('currentDate');
}
})
Template.date.onRendered(function(){
// this will update date every second, if you want update for every minute do 60 * 1000
Meteor.setInterval(function () {
Session.set(currentDate, new Date());
}, 1 * 1000);
})