在"dom-repeat"内切换"paper-dialog"



我在页面中有一个"paper-dialog"对象。如果它不在"dom-repeat"循环中,我可以通过按钮切换它。但是如果我把它放在一个循环中,"this.$.dialog.toggle((;"将引用null。

  <template is="dom-repeat" items="{{news}}" index-as"index">
    <paper-dialog id="dialog"><h3>{{item.date}}</h3></paper-dialog>
    <paper-button on-tap="toggleDialog">View {{item.name}}</paper-button>
  </template>

  toggleDialog: function(e) {
      this.$.dialog.toggle();
    }

知道为什么将对话框置于循环中后"this.$.dialog"变为空吗?

this.$不起作用。您必须调用等于Polymer.dom(this.root).querySelector();this.$$

另外,您有多个具有相同ID的元素,这绝对不符合HTML标准。

因此,您可以执行以下操作:

  <template is="dom-repeat" items="{{news}}" index-as"index">
    <paper-dialog id="dialog" indexDialog$='[[index]]'><h3>{{item.date}}</h3>
    </paper-dialog>
      <paper-button on-tap="toggleDialog" index$='[[index]]'>View {{item.name}}
    </paper-button>
  </template>

和切换对话框

  toggleDialog: function(e) {
      var index = e.target.getAttribute("index");
      this.$$('[indexDialog="'+index+'"]').toggle();
  }

您必须设置一些indetization(index属性(,然后在函数中您可以通过调用e.target.getAttribute来获取此属性,最后一步是通过调用this.$$('[indexDialog="'+index+'"]')在属性中找到indexDialog具有相同值的paper-dialog

我通过添加"数组选择器"找到了我的解决方案,因此"纸质对话框"在循环之外,只有 1 个实例。(我们将不同的数据输入其中(。

<array-selector id="selector" items="{{news}}" selected="{{selected}}"></array-selector>
<paper-dialog id="dialog" entry-animation="scale-up-animation"
exit-animation="fade-out-animation">......

并在切换功能内分配

  toggleDialog: function(e) {
    var item = this.$.news.itemForElement(e.target);
    this.$.selector.select(item);
    this.$.dialog.toggle();
  },

最新更新