ご存知のとおり、Polymerは、多くの合理的な理由により、エスケープされていないHTMLのバインディングを禁止しています。Polymer - innerHTML - 親要素のスタイルを適用できますか?
How to inject HTML into a template with polymer
How to display html inside template?
しかし私のプロジェクトでは、外部ソースからHTMLを適用するための要件でした。
コンポーネントは<エコー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をバインドしているので、私は< Pを使用する場合>または<強いです>または他の要素の場合、私は単純に親のコンポーネントスタイリングを使うことができないため、グローバルスタイルを使用する必要があります。
例えばので、親要素のスタイルを適用するにはどのような方法がある:
<parent-element>
<echo-html html$="{{some-nasty-html}}"></echo-html>
</parent-element>
は「いくつかの-厄介な-HTML」それに.parent-element
クラスが含まれますか?
.echo-html
クラスも削除できますか?実はこれは私の質問です:)
あなたがinnerHTMLプロパティが継承されたすべての要素にスタイルを適用しますか? –
DOM APIをラップすると、appendChildはその方法で自分の要素で動作しますが、他の要素を読み込んでいる場合は、importHrefを実行してからcreateElementを実行する適切な方法に注意してください – adaliszk