简体   繁体   English

Polymer在JavaScript中获取参数值

[英]Polymer get parameter value in javascript

I defined a custom element "wineItem" 我定义了一个自定义元素“ wineItem”

http://pastebin.com/VWh2Dk4J (I couldn't paste here the code) http://pastebin.com/VWh2Dk4J (我无法在此处粘贴代码)

When I want to call this element in a page I do something like this: 当我想在页面中调用此元素时,我会执行以下操作:

            <wine-item id_wine={{params.id_wine}}></wine-item>

The problem is that the value of "id_wine" inside the "wine-item" in the ready function is undefined, but in the html value appears properly. 问题是ready函数中“ wine-item”中的“ id_wine”的值未定义,但html中的值正确显示。

What can I do? 我能做什么?

Thank you. 谢谢。

The value of id-wine inside wine-item is undefined in the ready function because you did not explicitly create it as a property for the wine-item element. wine-item函数中, id-wine的值在ready函数中未定义,因为您没有明确将其创建为wine-item元素的属性。 Your element should look like this : 您的元素应如下所示:

 <link rel="import" href="../../../bower_components/polymer/polymer.html"> <dom-module name="wine-item"> <template> <span>{{id_wine}}</span> </template> <script> Polymer({ is:'wine-item', // Adding properties properties:{ id_wine:String // type can be Object Array or Number , refers to polymer doc }, ready: function() { // Now you can access id_wine in your ready function with `this` this.id_wine='idtest'; // $ allow to access element in the local DOM like <span id='id_wine'></span> // this.$.id_wine = this.id_wine; /* console.log(this.id_wine); console.log(this.$.id_wine)*/ } }); </script> </dom-module> 
 <wine-item id-wine="newIdTest"></wine-item> 

Now if your intention is to create an element with id ( Javascript id ) it is not possible, you can do it in the html when using the element like native html components 现在,如果您打算创建一个具有id( Javascript id )的元素,则不可能,当使用诸如本机html组件之类的元素时,可以在html进行操作

<wine-item id="id-of-my-element"></wine-item>
<dom-module name="wine-item">

is wrong, this should be: 是错误的,这应该是:

<dom-module id="wine-item">

Maybe that is the reason why your variable is undefined 也许这就是您的变量未定义的原因

Edit 编辑

To access to variable using javascript you will have to use as follows: 要使用javascript访问变量,您将必须使用以下方法:

this.yourVariable

Edit 编辑

My bad, I did not know that is possible to use "name" instead of "id". 我不好,我不知道可以用“名称”代替“ id”。

 <dom-module name="hello-test">
  <template>
    hello <span>{{name}}</span>
    <button on-tap="showData">show data</button>
  </template>
  <script>
    Polymer({
      is:"hello-test",
      name:{
        type:String
      },
      showData: function(){
        console.log(this.name);
      },
    });

  </script>
</dom-module>
  <hello-test name="Alex"></hello-test>

I got it. 我知道了。

The problem is that sometimes the ready function doesnt load when its ready. 问题在于,有时就绪函数在就绪时不会加载。

Just put a timer and it works! 只需放置一个计时器,它就可以工作!

    ready: function()
    {
        this.async(function ()
          { 
             ....
          },50);
    }

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

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