2015-04-02 173 views
8

如何值传递给模板事件如何将参数传递给流星中的模板事件?

的Html

<template name="Header"> 
<div class="testClass">Text1</div> // pass a = 1 
<div class="testClass">Text2</div> // pass a = 2 
</template> 

的Javascript

Template.Header.events({ 
'click .testClass':function(event, template){ 
    console.log(a) //print a values 
    } 
}); 

回答

7

您需要设置相应的数据背景下,利用孩子的模板,例如:

HTML

<template name="Header"> 
    {{> test text="Text1" a=1}} 
    {{> test text="Text2" a=2}} 
</template> 

<template name="test"> 
    <div class="test">{{text}}</div> 
</template> 

JS

Template.test.events({ 
    "click .test": function(event, template){ 
    console.log(this.a); 
    } 
}); 
+6

这并没有为我工作。但是,'template.data.a'没有。 – shmck 2015-08-19 01:54:47

+0

同样在这里 - 'template.data.a'会诀窍! – thomers 2015-09-14 16:20:55

+0

你确定你有完全相同的代码,因为'this.a'肯定会给我返回'1'。 – saimeunt 2015-09-14 16:34:29