问题
This seems like a very simple one, but somehow I am not sure how to do it. I want to send data to a Polymer component with the "core-collapse-open" event but this is not working:
<core-collapse on-core-collapse-open="{{loadDetails(data)}}">
{{data.Title}}
...
When I use the above code, the loadDetails
function in polymer is not hitting.
Polymer('custom-item', {
data: {},
ready: function () {
},
loadDetails: function (e, details, sender) {
debugger;
}
});
If I am not using the function syntax in the declarative syntax(as below), the loadDetails function hits.
<core-collapse on-core-collapse-open="{{loadDetails}}">
{{data.Title}}
...
How can I send parameters in events.
回答1:
on-core-collapse-open="{{loadDetails(data)}}"
This means: execute loadDetails(data)
and whatever the return value of this is will be bound as the event handler. Not what you want.
Also, the event handler function already receives parameters: the event object. You cannot pass it additional parameters. If the data
you want to pass refers to your this.data
attribute: the loadDetails
function already has access to it in the form of this.data
, so you don't need to pass it.
If you're trying to use the same handler function for two different events and pass additional parameters with each individual event: traditionally you'd do that with an anonymous function wrapper, and that's simply not possible using the declarative syntax.
回答2:
You can bind data
to an attribute of core-collapse
and then access the data using the target
argument of the event handler or the target
property of the event
argument or alternatively if data
is the model of the element anyway just access the TemplateInstance
(see full example at https://stackoverflow.com/a/24530099/217408)
回答3:
I got the answer from my another post. Even though I posted for different context, I understand I can use the same. Thanks Gunter for reply on that thread.
Polymer event parameters on repeat
来源:https://stackoverflow.com/questions/28104808/polymer-function-call-with-parameters