javascript - Angular2 dynamic stylesheet element -
i'm loading content beehance api in json, has text , style info make configured in editor.
the text contained in json has html tags , add [innerhtml]
{ text: '<div class="sub-title">lorem ipsum</div> <div class="paragraph">dolor sit amet</div>', ... }
and styles like
"paragraph": { "color": "#3b3b3b", "font_family": "arial,helvetica,sans-serif", "font_size": "12px", "line_height": "1.4em", "text_align": "left" }, "subtitle": { "color": "#000000", "font_family": "arial,helvetica,sans-serif", "font_size": "14px", "line_height": "1.6em", "text_align": "right" }
so there angular2 way can append generated css <style>
element?
tried style tag inside template don't know how interpolate there.
the style metadata doesn't work because styles dynamically fetched , vary depending on project.
what want this:
@component({ template: ` <style> .paragraph { {{styles.paragraph}} } </style> ` })
but interpolation doesn't work, there way that?
i found solution (it hack, works):
src/index.html
<!doctype html> <html> <head> <base href="/"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.png"> </head> <body> <style id="theme_styles"></style> <app-root></app-root> </body> </html>
src/app/style1.css.ts
export const style1 = ` body { background: red; } `;
src/app/style2.css.ts
export const style2 = ` body { background: blue; } `;
src/app/app/component.ts
import { component } '@angular/core'; import { style1 } './style1.css'; import { style2 } './style2.css'; @component({ selector: 'app-root', templateurl: './app.component.html' }) export class appcomponent { private themeelement: element = document.getelementbyid('theme_styles'); private isstyle1: boolean = false; constructor() { this.themeelement.innerhtml = style1; } ngoninit() { if (this.isstyle1) { this.themeelement.innerhtml = style1; } else { this.themeelement.innerhtml = style2; } } }
Comments
Post a Comment