polymer duplicate element content

穿精又带淫゛_ 提交于 2019-12-06 14:16:11

问题


I want to create custom element that displays its content, if content is tapped it will be displayed in paper-dialog. I have:

<paper-element name="my-element">
    <template>
        <div vertical layout center on-tap="{{contentClicked}}">
            <content></content>
        </div>
        <paper-dialog id="dialog">
            <content></content>
        </paper-dialog>
    </template>
    <script>
        Polymer({
           contentClicked: function(){
               this.$.dialog.toggle();
           }
        });
    </script>
</paper-element>

Content is added only to first div, not to paper-dialog. Is there some easy way to duplicate content from div to dialog?


回答1:


As mentioned in this elsewhere on Stack Overflow, you don't have to use the <content> insertion point to pull in the light DOM into your element. You could also use this.children to access the light DOM nodes from JavaScript.

There might be a simpler way of doing this, but just looping through all the children, cloning the nodes, and appending them multiple times does work. One thing to be aware of is that since you're adding clones of the original light DOM nodes to your element, any changes made to the light DOM during the lifetime of your page won't be reflected in those copies—each copy might get out of sync.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Polymer Demo</title>
  </head>
  
  <body>
    <script src="//www.polymer-project.org/webcomponents.min.js"></script>
    <link rel="import" href="//www.polymer-project.org/components/paper-dialog/paper-dialog.html">
    
    <polymer-element name="sample-element">
      <template>
        <div id="container" on-tap="{{contentClicked}}"></div>
        <paper-dialog id="dialog"></paper-dialog>
      </template>
      
      <script>
        Polymer({
          contentClicked: function() {
            this.$.dialog.toggle();
          },
          
          domReady: function() {
            for (var i = 0; i < this.children.length; i++) {
              this.$.container.appendChild(this.children[i].cloneNode(true));
              this.$.dialog.appendChild(this.children[i].cloneNode(true));
            }
          }
        });
      </script>
    </polymer-element>
    
    <sample-element>
      <h1>Hi!</h1>
      <div>Hello!</div>
    </sample-element>
  </body>
</html>


来源:https://stackoverflow.com/questions/27420695/polymer-duplicate-element-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!