Riggy Riggy - 1 year ago 97
TypeScript Question

Styled HTML content dynamically switched with tabs using Angular 2

I am attempting to create a reusable angular2 component that accepts an array of URLs to html files on my server and creates a content window with tabs to switch between "chapters", effectively swapping out the html and css inside the content window. I have tried all sorts of things including iframes but those don't work, the angular 1 ng-include work-arounds that I can find on StackOverflow but they have all since been deprecated, and the closest I've got is building a component that you can @Input html and it interpolates the content but style won't apply and angular strips out any style or script tags. Here is what I have tried.

In my parent component class:

htmlInput: string = "<h1>Why Does Angular make this so hard?</h1>";
cssInput: string = "h1 { color:red; }"

Parent Component HTML:

<app-html [html]='htmlInput' [css]='cssInput'></app-html>

My HTML Component:

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

selector: 'app-html',
template: '<div [innerHtml]=html></div>', //This works but no style
//template: '{{html}}', //This displays the actual markup on page
styles: ['{{css}}'] //This does nothing
//styles: ['h1 { color: red; }']//Also nothing
export class HtmlComponent implements OnInit {
@Input() html: string = "";
@Input() css: string = "";
ngOnInit() {


The result of this code is

Why Does Angular make this so hard?

But no red color. Maybe style is applied before the innerHtml is added to DOM? I don't know but just putting {{html}} results in displaying the actual markup with the h1 tags visible.

The reason I want to do it this way is that I have a bunch of HTML pages already created sitting in a folder on my server from before I angularized my site that all share a single style sheet. I'd like to just be able to flip through them like pages in a book without reloading the page and since there are so many and I'm likely to add more all the time, I'd really rather not create routing for every single one. (I already have routing for basic site navigation.)

Does anybody have a better suggestion for how to embed styled HTML into a page dynamically in the most recent version of Angular 2? At the time of this post we are in 2.0.0-beta.17.

OR... I already figured I may be approaching this issue from the entirely wrong angle. There must be a reason Angular is making this so difficult and deprecating all the solutions people have come up with so If anyone has a suggestion about how I could achieve the same results in a more angular friendly way I'd love to hear that too.

Thank you.

Answer Source

Angular2 rewrites the styles added to a component by including the dynamically added attributes like _ngcontent-yle-18 into the CSS selectors.

Angular2 uses this to emulate shadow DOM style encapsulation. These attributes are not added to dynamically added HTML (for example with innerHTML).


  • add styles to index.html because these styles are not rewritten by Angular2

  • set ViewEncapsulation.None because then Angular doesn't add the encapsulation emulation attributes

  • use /deep/ to make Angular2 ignore the encapsulation emulation attributes

See also Angular 2 - innerHTML styling

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download