简体   繁体   English

聚合物v 1.0 ..在带注释的事件处理程序中传递值

[英]polymer v 1.0 .. pass values in annotated event handlers

I was wondering how can I pass values to an event handler , when annotating them to an element in a template. 我想知道如何在将值注释到模板中的元素时将值传递给事件处理程序。

this works: 这有效:

 <button on-click="handleClick">

but this doesn't 但这不是

<button on-click on-click="handleClick(someValue)">

is there a way? 有办法吗?

It's not totally clear what you are trying to accomplish. 尚不清楚您要完成什么。 So you might want to clarify it a bit. 因此,您可能需要澄清一下。 But taking a guess, it sounds like I might still be able to help you... 但是,想一想,听起来我仍然可以为您提供帮助...

Look at this Stack Overflow question , the accepted answer and my comment. 查看这个Stack Overflow问题 ,可接受的答案和我的评论。

Amit points out you could use HTML5 custom ( data- ) attributes. 阿米特指出,您可以使用HTML5自定义( data- )属性。 Like this: 像这样:

<paper-button id="foo" on-tap="bar" data-args="foo,some other value,2">Click</paper-button>
...
<script>
(function() {
  Polymer({
    is: 'example',
    properties: {...},
    bar: function(e){
      var args = e.target.getAttribute('data-args').split(',');
      // now args = ['foo', 'some other value', '2']
    }
  });
})();
</script>

Which worked for me. 对我有用。 However, in my particular use case, I had to use: Polymer.dom(e).path[2].getAttribute('data-args') . 但是,在我的特定用例中,我必须使用: Polymer.dom(e).path[2].getAttribute('data-args')

To learn more, you can read this reference on Event Retargeting . 要了解更多信息,您可以阅读有关事件重定向的参考

If the handler is scoped inside of the host element you can add a custom property to the element and supply an argument in the attribute that binds to that property. 如果处理程序的作用域位于host元素内部,则可以向该元素添加一个自定义属性,并在该属性中提供一个绑定到该属性的参数。 You will only be able to supply one argument to the property, you can use an object. 您将只能为该属性提供一个参数,可以使用一个对象。 However it is possible to handle the Annotated Events outside of the host element structure and this will mean something else. 但是,可以在宿主元素结构之外处理带注释的事件,这将意味着其他事情。 Using the event that was passed in works always. 使用传入的事件始终有效。 With a reference to the element itself you will also be able to access these properties and bindings as well as the attributes. 通过引用元素本身,您还可以访问这些属性和绑定以及属性。

<dom-module id="awesome-element">
    <template>
        <h1>Awesome Element</h1>
        <content></content>
    </template>
    <script>
        polymer({
            is:'awesome-element',
            properties{
                onearg: {
                    type: String,
                    value: ''
                },
                handleClick: function(e){
                    console.log(this.onearg);

                }
            }
        });
    </script>
<dom-module>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM