Set sass variable value in Angular 7

后端 未结 6 2624
悲哀的现实
悲哀的现实 2021-02-19 22:11

I have been working with angular for the last few weeks, and now I have a requirement to dynamically style a public site. The site admin set various color codes as well as a log

6条回答
  •  再見小時候
    2021-02-19 22:45

    I don't think that what you want will be possible to do... Angular processes the SASS files during application build and writes all the common results into a plain old css file. The component-specific stuff will get generated as javascript that, in turn, will apply your styling at run time.

    Hence all the SASS variables you need to set up have to be present at compile time.

    What you can do, though, is to pre-define your setup in Angular components and then toggle it based on an input (from your DB or wherever else), like so:

    // your.component.ts
    @Component({
      // ... component stuff
      styles: ['h1.option1 {color: red;}', 'h1.option2 {color: blue;}'],
      template: `
        

    Hey there, I'm styled!

    No option received

    ` }) export class YourComponent { optionSelection$: Observable; constructor(yourService: YourService){ this.optionSelection$ = yourService.getYourOption().pipe(startWith(null)); } }

    Hope this helps a little :-)

提交回复
热议问题