style not applied to dynamic content in polymer we

2019-08-11 07: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)

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-11 07:26

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>

查看更多
混吃等死
3楼-- · 2019-08-11 07:48

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>';
查看更多
登录 后发表回答