Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6, color from css file in ngStyle directive

I would like to use my color which is defined in CSS file for my ngStyle directive (which is in HTML).

This is what i've got in my HTML:

 [ngStyle]="{backgroundColor: isDarkStyle() ? '#29434e' : isNormalStyle() ? '#cfd8dc' : isLightStyle() ? 'white': ''}"

This my CSS file with colors:

//colors for background

$light-background: white;
$normal-background: #cfd8dc;
$dark-background: #29434e;

But i want this:

[ngStyle]="{backgroundColor: isDarkStyle() ? '$dark-background' : isNormalStyle() ? '$normal-background' : isLightStyle() ? '$light-background': ''}"

How can I achieve this result? Thanks for help :)

like image 895
Kacper Kapela Avatar asked Jul 17 '18 08:07

Kacper Kapela


2 Answers

Use [ngClass]

See stackblitz:https://stackblitz.com/edit/hello-angular-6-refjye?file=src%2Fapp%2Fapp.component.html

.light{
background: white;
}
.normal{
background: #cfd8dc;
}
.dark{
background: #29434e;
}

in Html

<p [ngClass]="isDarkStyle() ? 'dark' : isLightStyle() ? 'light': isNormalStyle()?'normal':''">
  Start editing to see some magic happen :)
</p>
like image 117
לבני מלכה Avatar answered Sep 27 '22 19:09

לבני מלכה


try solution

As I understood your queston:

HTML:

<h1 [ngStyle]="{backgroundColor: isDarkStyle() ? 'red' : isNormalStyle() ? 'green' : isLightStyle() ? 'white': ''}">Text</h1>

TS:

isDarkStyle() {
    return false
  }

  isNormalStyle() {
    return true
  }

  isLightStyle() {
    return true;
  }
like image 40
Akj Avatar answered Sep 27 '22 19:09

Akj