Download css file dynamically for theme

I have three css files with different color themes, for example. theme1.css theme2.css theme3.css

I want to download them depending on the selected category. Is it possible to load css files dynamically in angular2? What is the right way to handle this?

Thanks!

+5
source share
1 answer

I am not sure if this is correct. But you can try this -

import { Component, Inject } from '@angular/core'; import { DOCUMENT } from '@angular/platform-browser'; @Component({ }) export class SomeComponent { constructor (@Inject(DOCUMENT) private document) { } LightTheme() { this.document.getElementById('theme').setAttribute('href', 'light-theme.css'); DarkTheme() { this.document.getElementById('theme').setAttribute('href', 'dark-theme.css'); } } 

Link: https://angular.io/docs/ts/latest/api/platform-browser/index/DOCUMENT-let.html

See if that helps.

+10
source

All Articles