Polymer 1.0:如何将参数从属性传递给Polymer函数? [英] Polymer 1.0: How to pass an argument to a Polymer function from an attribute?

查看:113
本文介绍了Polymer 1.0:如何将参数从属性传递给Polymer函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过<template>内部的element属性将参数传递给Polymer函数?

Is there a way to pass an argument to a Polymer function from an element attribute inside its <template>?

<script src="http://www.polymer-project.org/1.0/samples/components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html" />
<dom-module id="example-element">
  <template>
    ...
    <paper-button id="foo" on-tap="bar">Click</paper-button>
    ...
  </template>
</dom-module>
<script>
  (function() {
    Polymer({
      is: 'example-element',
      properties: {...},
      bar: function(arg){
        // Do stuff using argument arg
      }
    });
  })();
</script>

背景研究

我已经仔细阅读了文档,该文档对此问题保持沉默.它没有说您是否可以.但是当我尝试时,它会失败.但是也许我做的不正确.所以我需要一些帮助.

Background Research

I have combed through the documentation which appears silent on the matter. It doesn't say whether you can or can not. But when I try it, it fails. But maybe I'm not doing it correctly. So I need some assistance.

我遇到的唯一一件事是事件监听器才能接受我想传递的论点.说一个idname.

The only thing I have come across is event listeners which doesn't seem to be able to take the arguments I want to pass. Say, an id or a name.

我尝试(失败)做类似的事情:

I have tried (unsuccessfully) doing things like:

<paper-button id="foo" on-tap="bar('foo')"> Click </paper-button>

但似乎没有任何作用.

事件侦听器的想法行不通,因为它们限制了参数,而我无法得到我需要的id.

The event listeners idea doesn't work because they limit the arguments and I can't get the, say, id I need.

推荐答案

您可以改用HTML5数据属性.尝试这样:

You could utilize HTML5 data attributes instead. Try 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>

这篇关于Polymer 1.0:如何将参数从属性传递给Polymer函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆