简体   繁体   English

聚合物-dom-if模板中的访问元素

[英]Polymer - Access element in dom-if template

<dom-module id="my-view1">
  <template id="methodScope">
    <paper-dialog id="modal1">
      <paper-swatch-picker></paper-swatch-picker>
      <paper-button dialog-confirm autofocus>Fertig</paper-button>
    </paper-dialog>
    <paper-toast id="copiedColor" text="Farbe in die Zwischenablage kopiert!"></paper-toast>
  </template>

  <template id="create" is="dom-if" if="{{mapChosen}}" restamp="true">
    <iron-ajax auto url$="{{mapJson}}" handle-as="json" last-response="{{mapData}}"></iron-ajax>

    <paper-button id="button1">Data</paper-button>
  </template>

  <script>
    Polymer({
      is: 'my-view1',
      attached: function(event) {
        dialog = this.$.modal1;
        colorPick = this.$.colorpicker;
        var self = this;
        this.$.colorpicker.addEventListener('color-picker-selected',function() {
          self._alertColor()
          self.selectedColor = colorPick.color;
          self.copyToClipboard(colorPick.color);
          dialog.close();
          console.log("fired");
        })
      }
      var button = this.$.button1;
    });
 </script>
</dom-module>

Accessing elements outside of the dom-if template works perfectly, but no matter what I do, I cannot access them inside of it. 访问dom-if模板外部的元素可以很好地工作,但是无论我做什么,我都无法在其内部访问它们。 I already tried using this.async and this.$$ - all returning undefined. 我已经尝试过使用this.asyncthis.$$ -全部返回undefined。 How would I be able to access the button in the template with the dom-if and id "create"? 如何使用dom-if和ID为“ create”访问模板中的按钮?

I've cut some of the code, so if you don't see some functions or other things, that's because of that 我剪切了一些代码,所以如果您看不到某些功能或其他内容,那是因为

Try listening on dom-change event. 尝试收听dom-change事件。 Below is an example for dom-repeat but same should work with dom-if also 以下是dom-repeat的示例,但同样适用于dom-if

 <base href="https://polygit.org/components/"> <script src="webcomponentsjs/webcomponents-lite.min.js"></script> <link rel="import" href="polymer/polymer.html"> <dom-module id="repeat-completion"> <template> <style></style> <template is="dom-repeat" items="{{items}}" as="item" on-dom-change="_domComplete"> <div id$="num{{index}}">{{item}}</div> </template> </template> </dom-module> <script> Polymer({ is: 'repeat-completion', properties: { items: { type: Array, value: function() { return [1, 2, 3, 4, 5] } } }, _domComplete: function(e) { if (e.returnValue) console.log(this.$$("#num1")); } }) </script> <repeat-completion></repeat-completion> 

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

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