如何在Meteor中将参数传递给模板事件?

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
  }
});
1个回答

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
你确定你有完全相同的代码吗?因为对我来说,this.a 明显返回 1 - saimeunt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接