【发布时间】:2017-03-28 15:35:45
【问题描述】:
您可能知道 - Polymer 出于许多合理的原因禁止未转义的 HTML 绑定。
How to inject HTML into a template with polymer
How to display html inside template?
但是在我的项目中,需要应用来自外部源的 HTML。
因此我实现了一个名为
<dom-module id="echo-html">
<template>
<style>
</style>
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'echo-html',
properties: {
html: {
type: Object,
value: '',
observer: '_refreshHtml'
},
},
_refreshHtml: function() {
if (this.html) {
var value = '';
var container = Polymer.dom(this).parentNode;
if ((this.html.constructor == Array) && (this.html.length == 1)) {
value = this.html[0];
} else {
value = this.html;
}
if (this.parentNode) {
this.outerHTML = value;
}
this.scopeSubtree(container, true);
$(this).removeClass('echo-html');
}
},
});
})();
</script>
我的问题是这个组件完全绑定了纯 HTML,所以当我想使用
或 或任何其他元素时,我不能简单地使用父组件的样式,因此需要使用全局样式.
有没有办法应用父元素的样式,例如:
<parent-element>
<echo-html html$="{{some-nasty-html}}"></echo-html>
</parent-element>
“some-nasty-html”会在上面包含.parent-element 类吗?
有什么方法可以删除.echo-html 类吗?其实这是我的问题:)
【问题讨论】:
标签: polymer polymer-1.0 shadow-dom