5

What I have right now is a page with dark theme in index.html:

<base href="/">
<html>
<head>
    <title>XXX</title>
</head>
<body>
    <link rel="stylesheet" type="text/css" href="assets/dark_room.css">
    <my-app>Loading...</my-app>
</body>
</html>

I also have a bright "light_room.css" theme that is required to implement, and user could switch theme base on needs. I am wondering how could I achieve this?

I thought it could be done by having an url parameter so that when user type like ?light_room=True at the end of url, the page will load light_room.css. However I think it can only be done in normal templates but not in index.html.

Does anyone have similar experience in conditionally loading css file in index.html?

2
  • Have you tried it this way? Let your backend generate different index.html depending on the URL-Parameters. Commented May 30, 2017 at 13:21
  • @Niklas I think it could be an approach but I don't know how. since setting up URL-parameters is usually done in the .ts file but index.html does not have one. Commented May 30, 2017 at 14:15

1 Answer 1

2

The point is that theming is usually just some different colors. You can add all themes to one css. my-app > .dark gets different colors and the class will be added dynamically in the code.

Here is a more detailed example how I use it:

app-theme.scss:

@import '~@angular/material/theming';

@include mat-core();
$words-app-primary: mat-palette($mat-teal, 300);
$words-app-secondary: mat-palette($mat-indigo, 500);
$words-app-warn: mat-palette($mat-red, 500);

$words-app-theme: mat-light-theme($words-app-primary, $words-app-secondary, $words-app-warn);
@include angular-material-theme($words-app-theme);

app-words-root > md-sidenav-container.dark {
  $words-app-theme-dark: mat-dark-theme($words-app-primary, $words-app-secondary, $words-app-warn);
  @include angular-material-theme($words-app-theme-dark);
}

app.component.html:

<md-sidenav-container [class.dark]="useDarkTheme"> ... </md-sidenav-container>

non angular version

app.component.js

import { DOCUMENT } from '@angular/platform-browser';

class AppComponent {
  constructor(@Inject(DOCUMENT) document: any) {
    let head  = document.getElementsByTagName('head')[0];
    let link  = document.createElement('link');
    link.rel  = 'stylesheet';
    link.type = 'text/css';
    link.href = 'assets/dark_room.css'; // you may want to get this from local storage or location
    head.appendChild(link);
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for answering but I am not allowed to touch the css files, so....I still need to find a way to choose those two files somehow
This will be similar problem as for title - angular 2 does not have functions for dom manipulation outside of <app>. For title they offer a Title service. But not for loading css.
Yeah, if I can include function it could be doable.
It comes up my mind that can I just create 2 index.html and load the index.html after the url, but when I type in "/index_light.html", the page always shows "loading...."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.