2016-03-31 10 views
0

現在の日付を表示したい。私は次のコードを書いたが、変数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 
    }); 
} 
+2

以下のようなもの...' Template.date.helpers({値:値}を書くことができます) '、さらには' Template.date.helpers({value}) 'のようなものです。 –

答えて

1

template.date.helpers各返却結果のための方法と、オブジェクトを必要とします。こんにちはテンプレートのカウンターヘルパーと同じように:

Template.date.helpers({ 
value: function() { 
    return new Date(); 
} 

});

+0

これは初期値を返します。時間は動的に変化しません。 – webdeb

+0

ヘルパーが実行されるたびに新しい日付オブジェクトが返されます。あなたはそれを文字列化するか、またはJavaScriptパラメータを使ってそれをフォーマットすることができます。これは反応変数ではないため一度だけ発火します。 –

1

あなたは現在の時刻を表示したい場合は、あなたが `helpers`関数にオブジェクトを提供する必要があり

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); 

}) 
関連する問題