1

If I have a template as such:

<div [innerHTML]="html"></div>

and in the component:

    public html = "<div class='title'>Title</div><p>etc. etc.</p>";
    public theme = {
       title: { 
          styles: {
             letterSpacing: '1px',
             fontWeight: 'bold', 
             color: 'gray' 
         }
      }
   };

How can I apply the CSS in theme.title.styles to the rendered HTML?

One idea I had is to dynamically define the styles in the component metadata, but I don't know whether this is possible.

2 Answers 2

2

Well I think you can use ngStyle

Try this hope it will work :)

<div [ngStyle] ="getStyle()" [innerHTML]="html"></div>

And then declare a function in component as

getStyle(){
  let styleObject =  theme.title.styles;// or your style object howe so ever you want to define
  return styleObject;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Since you use HTML, you can't use ngStyle.

From there, you have two solutions, but given your theme object structure, this only leaves one :

public html = `<div class="title" id="YouNeedACustomId">Title</div><p>etc. etc.</p>`;
let el = document.getElementById('YouNeedACustomId');
for (let prop in theme.title.styles) { el.style[prop] = theme.title.styles[prop]; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.