1

I'm trying to include css styles sent by a backend into my component template.

My server response looks like this:

serverResponse: {
   content: '<div class="myClass classX">hello world</div>',
   styles: '.myClass{color:red} .classX{...}  ....';
}

(I know it's not a good idea to get css styles from a backend, but unfortunately I can't change it ;-)

My component template looks like this:

<style>
   <!-- serverResponse.styles should be in here. But how? -->
</style>
<div [innerHTML]="serverResponse.content">
   <!-- Binding to innerHTML works! -->
</div>

I have already set encapsulation to "ViewEncapsulation.None" in my component to enable styling with the <style> element.

What I have already tried is something like this:

<style [textContent]="serverResponse.styles">

But binding to textContent does not work...

I'd really appreciate your help!

3
  • Maybe you should load entry component dynamicly? Commented Jan 15, 2017 at 15:30
  • Why doesn't <style [innerHTML]="serverResponse.styles"> work? What errors do you get? Commented Jan 17, 2017 at 21:02
  • @MrLister The style element is just not created Commented Jan 18, 2017 at 7:32

2 Answers 2

1

First you should update your class property with your style from server response.

Then bind your style class property using style binding with your template.

Sign up to request clarification or add additional context in comments.

2 Comments

I do agree that this is a better approach, I would highly recommend this over trying to dynamically add CSS to the page styles.
What do you mean with "update your class property"? The styles I get from the backend could contain multiple classes... E.g. serverResponse.styles = ".myClass{...} .classX{...}"
0

I hesitated a bit since I don't think appending individual css rules to the page is good practice, but I will attempt to answer the question.

$(function() {

  function handleElementResponse( html, css ) {
    console.log( $("style") );
    
    $("style").append( css );
    $("#mydiv").append( html );
  };
  
  handleElementResponse(
    "<div class=\"myClass\">hello world</div>",
    ".myClass{color:red}"
    );
    handleElementResponse(
    "<div class=\"myClass2\">hello world 2</div>",
    ".myClass2{color:blue}"
    );
});
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <style></style>
</head>

<body>
  <div id="mydiv">
     <span class="someClass">Hello</span>
  </div>
</body>
</html>

My humble opinion is that instead you should either:

  1. Add the styles to the element directly using the "style" attribute
  2. If possible, define your styles beforehand and just apply the classes to the html elements.

Hope this helps

1 Comment

Ok this might work... But I'm wondering if there is a more Angular2-specific solution.

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.