我希望能夠使用模板事件處理函數設置/獲取模板對象的數據。我試圖在模板呈現時設置一個變量,並希望稍後可以訪問這個變量,在這種情況下,當模板中的某個元素被用戶點擊時,但它不起作用:如何獲取模板對象數據以保持流星模板?
<template name="fooBar">
<div class="some_element">CLICK ME</div>
</template>
Template.fooBar.rendered = function(){
this.templateVar = "Hello";
}
Template.fooBar.events({
'click .some_element': function(e,t){
alert(this.templateVar); // Should say 'Hello', but is 'undefined'.
}
});
您可以使用會話? – Ethaan 2015-04-04 19:40:09
t.templateVar或Template.instance()。templateVar – Sindis 2015-04-04 19:41:12
Template.instance在渲染和事件處理程序上獲得了[不同行爲](https://groups.google.com/forum/#!topic/meteor-talk/PrzfPlYKYjk) – Ethaan 2015-04-04 19:41:47