【发布时间】:2019-12-09 19:36:44
【问题描述】:
1) 如果我想在 dom-if 条件下选择元素并运行观察者,我该如何实现该场景,即我有下拉列表,它包含在 dom-if 和页面加载时,一些观察者正在将标志更改为 true,这会触发dom-if 条件来呈现该下拉列表,但问题是当页面加载时,我在观察者中绑定下拉选项,该选项获取元素 this.$.elementID || this.$.querySelector('#elementID') 并绑定它,所以我没有得到该元素,但在 ui 中它显示没有选项的空白下拉列表所以我猜元素没有被选中。
请帮忙?
<template is="dom-if" if="[[flag]]" restamp="true">
<dropdown id = "elementID"></dropdown>
</template>
JS:
properties:{
list: {
type: Array,
notify: true,
value: [{label:"f1",value:"f1"},{label:"f2",value:"f2"}]
}
}
static get observers() {
return [
'_bindDrop(list)',
];
}
_bindDrop(list) {
const select = this.$.querySelector('#elementID');
if (select) {
select.options.length = 0;
list.forEach(function (item) {
const option = document.createElement('option');
option.textContent = item.label;
option.value = item.value;
select.appendChild(option);
});
}
}
或
2) 如何在 dom-if 条件下为元素添加动态观察者方法,如果元素获得标志为 true 则添加观察者方法?
【问题讨论】:
标签: javascript polymer polymer-2.x observers mutation-observers