style not applied to dynamic content in polymer web-component

人盡茶涼 提交于 2019-12-11 10:51:11

问题


I recreate an h2-element here:

<link rel="import" href="../../js/lib/polymer/polymer.html">
    <dom-module id="x-custom">
    <style>
        h2 { color: green; }
    </style>
    <template>
        <div id="content">
            <h2>TEST</h2>
        </div>
    </template>

    <script>
    (function() {
        Polymer({
            is: 'x-custom',
            ready: function() {
                this.$.content.innerHTML = '<h2>TEST 2</h2>';
                this.updateStyles();
            }
        });
    })();
    </script>
</dom-module>

If I skip the ready-function "TEST" is green, but not "TEST 2". Thought updateStyles() may fix this, but didn't. Any ideas why this doesn't work? (Polymer 1.0, Chrome 44)


回答1:


You can't use innerHTML as usual, you need to do this with Polymers own DOM API. This works:

Polymer.dom(this.$.content).innerHTML = '<h2>TEST 2</h2>';



回答2:


How about of binding data?

<dom-module id="x-custom">
<style>
    h2 { color: green; }
</style>
<template>
    <div id="content">
        <h2>{{test}}</h2><!-- this will be in green color -->
    </div>
</template>

<script>
(function() {
    Polymer({
        is: 'x-custom',
         properties:{
          test:{
            type:String,
            value:"test 1"
          },
         },
        ready: function() {
          this.test = "test 2"
        }
    });
})();
</script>



来源:https://stackoverflow.com/questions/32231103/style-not-applied-to-dynamic-content-in-polymer-web-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!