How to pass parameters to template event in meteor?
Asked Answered
W

1

8

How to pass values to template events

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
  }
});
Worth answered 2/4, 2015 at 11:15 Comment(0)
T
7

You need to set the appropriate data context, using child templates for example :

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);
  }
});
Thorpe answered 2/4, 2015 at 11:29 Comment(2)
This didn't work for me. However, 'template.data.a' did.Cartier
Are you sure you have the exact same code, because this.a definitely returns 1 for me.Thorpe

© 2022 - 2024 — McMap. All rights reserved.