如何将值传递给模板事件

Html

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

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

最佳答案

您需要使用子模板来设置适当的数据上下文,例如:

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

关于javascript - 如何将参数传递给 meteor 中的模板事件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29411570/

10-10 10:41
查看更多