2015-10-28 44 views
0

我定义了一个自定义元素“wineItem”聚合物可以在javascript

http://pastebin.com/VWh2Dk4J(我不能在这里粘贴代码)

当我想要把这个元素在一个页面中我做了什么样的参数值这样的:

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

的问题是,在准备功能“id_wine”的“酒项目”里面的价值是不确定的,但在HTML值正确显示。

我该怎么办?

谢谢。

回答

0

我明白了。

问题是,准备就绪的功能有时在准备就绪时无法加载。

只要放一个计时器,它的工作原理!

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

wine-item中的id-wine的值在就绪函数中未定义,因为您没有明确将其创建为wine-item元素的属性。你的元素应该是这样的:

<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>

现在,如果你的目的是要创建一个ID(Javascript id)的元素,它是不可能的,你可以在html做使用元素时像本地HTML组件

<wine-item id="id-of-my-element"></wine-item> 
+0

感谢您的回答,但它不起作用。 “id_wine”是元素的一个参数,所以我不想在ready函数中给它一个值。我只需要取这个参数的值。 – Xinin

+0

明确你想要达到什么目的,如果你想得到一些帮助 –

+0

dat人.... – Xinin

0
<dom-module name="wine-item"> 

是错误的,这应该是:

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

也许这就是为什么你的变化是不确定的

编辑

要使用JavaScript,你将不得不使用如下访问变量的原因:

this.yourVariable 

编辑

我的不好,我不知道可以使用“名称”而不是“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>