3

In my application I have different CSS file like fire.css, lake.css each giving different look to complete application.

Currently, I implemented with only 1 file main.css and added this file to
index.html

<link rel="stylesheet" href="resources/styles/main.css">  
<link rel="stylesheet" href="resources/styles/themes.css">
<link rel="stylesheet" href="resources/styles/common.css">
<link rel="stylesheet" href="resources/styles/plugins.css">

Now I wanted to change this dynamically as user select from the drop-down list.

My Idea: Copy all css files to app folder and add themes there.
Folder structure is

app
|-----app.component.ts
|-----app.routes.ts
|-----main.css
|-----lake.css
|-----Login
|-----Other Components...

I added css to styleUrls in app.components.ts App component now is

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({

    selector: 'workshop-app',    
    template: `
        <body>
           <router-outlet></router-outlet>
       </body>
    `,
    directives : [ ROUTER_DIRECTIVES ],
    styleUrls : ['app/lake.css']
})
export class AppComponent { }

Contents from Lake.css file is added to style tag under but not making changes in the app.

1

2 Answers 2

6

add this to the app.component.html

<head>
<link id="theme" rel='stylesheet' href='demo.css'>    
</head>  

add this to app.component.ts

import { Component, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';    
constructor (@Inject(DOCUMENT) private document) { }
ngOnInit() {
  this.document.getElementById('theme').setAttribute('href', 'demo2.css');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer !!! Project is submitted without this feature I will try it in spare time.
happy to help you, wish you luck
-1

Use fileReplacements in Angular.json file:

"configurations": {
  "yourproject": { ... },
  "staging": {
    "fileReplacements": [
      {
        "replace": "src/styles.css",
        "with": "src/new-styles.css"
      }
    ]
  }
}

You can check the usage better here: https://angular.io/guide/build

1 Comment

he is asking to change styles dinamically when the user inputs some actions, not in build time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.