178

I am passing html as innerHtml to my view. Below is my view

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

if I pass the below code, it is working fine.

this.someHtmlCode = "<div><b>This is my HTML.</b></div>"

if I pass the below code which contains color, it is not working.

 this.someHtmlCode = '<div style="background-color: blue;"><b>This is my HTML.</b></div>';
3

2 Answers 2

356

This behavior you're getting is normal. The class added to innerHTML is ignored because by default the encapsulation is Emulated. Which means Angular prevents styles from intercepting inside and outside of the component. You should change the encapsulation to None in your component. This way, you'll be able to define classes wherever you want: inside styles or in a separate .css, .scss or .less style-sheet (it doesn't matter) and Angular will add them to the DOM automatically.

import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'example',
  styles: ['.demo {background-color: blue}'],
  template: '<div [innerHTML]="someHtmlCode"></div>',
  encapsulation: ViewEncapsulation.None,
})
export class Example {
  private someHtmlCode = '';

  constructor() {
    this.someHtmlCode = '<div class="demo"><b>This is my HTML.</b></div>';
  }
}
Sign up to request clarification or add additional context in comments.

17 Comments

If I pass just style without class It is not working
@Chatra You may want to check out this
ViewEncapsulation is a part of angular/core: import { ViewEncapsulation } from '@angular/core';
Make sure you import ViewEncapsulation from @angular/core and NOT from @angular/compiler/src/core. Otherwise, you will end up with a mysterious error - "Cannot find module 'tslib'"!
It's worth noting that changing ViewEncapsulation to None can have pretty far-reaching consequences. This applies your styles globally - so if you set something like a div tag to have a background color, this will change all divs in your app to have that background color. I would steer clear of changing the ViewEncapsulation unless you absolutely have to.
|
-5

Instead of an inline style, I put the style in a class.

Not sure if using class is an option for you or not, but here's a Plunker demo.

HTML:

this.someHtmlCode = '<div class="demo"><b>This is my HTML.</b></div>'

Define the class demo at the global root css file, usually it's called styles.scss.

CSS:

.demo{
    background-color: blue;
}

5 Comments

it is not working, Is there any difference if my demo css is in less file?
This works if the css is in the global styles.css file, but not in a non-global css file. It would be helpful if someone could explain the difference, but in the meantime I thought I'd post a comment to make others aware that getting this working depends on such details.
my-angular-component.html has <div class="my-static-div" [innerHTML]="<div class='my-dynamic-div'>" (click)="handleStaticClick($event)" ... then in my-angular-component.css use .my-static-div ::ng-deep .my-dynamic-div { color: blue; } and my-angular-component.ts public handleStaticClick(event) { if (event.srcElement.classList.contains('my-dynamic-div')) { this.handleDynamicClick(); }}
I solved my issue with this awnser by placing the selector ::ng-deep before the class.
Actually I solved my problem doing this. I defined a class .mono { font-family: monospace !important; } in my style.scss and then used it as usual: <h2 class="mono">Title</h2>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.