1

What about dynamic styling.

Imagine you're deploying a unique plaftorm for multiple clients. (SAAS platform)

You deploy ONE webapp folder but these webapp must apply a different theme (color) for each client (exemple configuration : loading colors by domain name).

In angular 2, there is scss but the scss is a compiled language -> so you need to compile each time for each client to compile N webapps. It takes time and it's difficult to maintain.

So the only solution i see: - compile scss at runtime (via remote server call, fe:jsass, or js) when loading app and inject the generated css file in head section.

but i think it's an anti-pattern with the (s)css file for angular2 components. Moreover the generated file will contains components style that will apply on entire page instead even if component is not initialized.

Have you got any framework or other solution ?

3
  • SCSS is only compiled once (When you build your app) into CSS file. Same goes for the Typescript files, compiled and minified into one JS file. Commented Apr 6, 2017 at 12:54
  • You should create a custom RootRenderer, that will handle compiling sass or use custom css files. To do that, you will need to create a new platform, that's a big task I never did. See how it works in browser platform. And there is a speach about custom renderers at ng-europe Commented Apr 6, 2017 at 13:15
  • great i will have a look at it. Commented Apr 6, 2017 at 13:40

1 Answer 1

1

You can achieve this creating a Service that return the colors of some structures that you want to change and inject it into the Components

Example inside the component html:

<div [style.background-color]="themeService.getNavbarColor()"></div>

And do some logic to get the pattern for each user when App starts and insert into the ThemeService.

Full Example:

import { Component } from '@angular/core'

@Component ({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
})
export class MyComponent {

    constructor( private themeService: ThemeService){}
}

@Injectable()
export class ThemeService {
    backgroundColor: string

    getNavbarBackgroundColor: string() {
        return this.backgroundColor;
    }

    someLogicToGetTheme() {
        //do stuff
    }

    ngOnInit() {
        this.someLogicToGetTheme()
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes but it's pity to have to specify colors in non styles files. In the Google Web Toolkit (GWT) world you can evaluate value in GSS file. Im' surprised that there is no equivalent.
Indeed it's pity, but I don't know another way to dinamically change the CSS in production on Angular
I am trying to achieve the same thing, this project has dynamic themes github.com/akveo/nebular but the sass code is rather complicated @cassiano

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.