javascript - 使用 Polymer Templatizer 标记模板
问题描述
如果需要,我想添加模板的内容,并在不需要时将其删除。我的代码基于聚合物的 dom-if 实现
这是添加模板内容的代码
if (!this.ctor) {
this.templatize(this);
}
if (parentNode) {
const parent = Polymer.dom(parentNode);
if (!this._instance) {
this._instance = this.stamp();
const targetNode = parent.firstElementChild;
const newNode = this._instance.root;
parent.insertBefore(newNode, targetNode);
} else {
const children = this._instance._children;
if (children && children.length) {
const lastChild = Polymer.dom(this).previousSibling;
if (lastChild !== children[children.length - 1]) {
for (let i = 0, node; i < children.length && (node = children[i]); i++) {
parent.insertBefore(node, this);
}
}
}
}
}
这是删除模板内容的代码:
if (this._instance) {
const children = this._instance._children;
if (children && children.length) {
const parent = Polymer.dom(Polymer.dom(children[0]).parentNode);
for (let i = 0, n; i < children.length && (n = children[i]); i++) {
parent.removeChild(n);
}
}
if(this.restamp) {
this._instance = null;
}
}
因此,如果我将 restamp 属性设置为 true 一切正常。但是,如果我将其设置为 false,则不会显示某些内容。
上面的代码中不会插入 dom-repeate 模板的内容
parent.insertBefore(node, this);
因为 node 只包含 #document-fragments 而不是 dom-repeat 插入的内容。
我试图达到正确的内容,但我失败了。是否有可能插入正确的内容,即使它包含 dom-repeat、dom-if 等?
谢谢
解决方案
推荐阅读
- regex - Notepad++ 正则表达式去除器在此文本之后的任何内容
- javascript - 我可以为按钮后面的画布设置动画并绘制像素吗?
- twitter - 我怎样才能获得某一天最受欢迎的推文?
- laravel - 在两个不同的 Gitlab CI 中连接到同一个数据库
- java - micronaut - java.lang.IllegalAccessError graalvm 本机编译外部依赖
- java - 如何计算给定 String[][] 和 String[][] 预期返回的平均成绩
- java - 如何使用圆点幻灯片注册活动
- python-3.x - wxPython 需要什么才能成功运行?
- python - 如何读取文件并将内容存储到二维矩阵中?
- javascript - XPath:选择包含规范化文本的节点,不包括祖先节点