Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have one question...

Sure, that everybody know, that if you want conditional styling: you must use ng-class or ng-style construction.

But.

For example: i'm admin. And i want to change color of my application with custom color from colorpicker. How can i change some code in css?

for example i have this line in style.css:

body{
  background: #ffffff;
}

(also all tags like a, h1 etc implement some color)

and in controller i change this #ffffff to #000000.

what is the best way, to change this color in css, without using ng-class or ng-style on each tag in each controller?

share|improve this question

3 Answers 3

The best way is generate a file like color.css with all css rules with color, background-color, border-color etc. overridden. But angularjs will not be enough.

color-default.css

body {
    background: #fff;
}

color.css

body {
    background: #f00;
}

Full JS way
Add class on every element you want to override. Create class for every properties like so:

.skin-color { color: {{color}}; }
.skin-background-color { background-color: {{color}}; }
.skin-border-color { border-color: {{color}}; }
etc..

Apply class on your html where you want:

<h1 class="skin-color">My title</h1>
<p>Hello I'm online!</p>
<p class="skin-background-color">No difference!</p>
<p><a href="#">I'm link</a></p>

You can save the color variable in localStorage for example.
Démo: http://codepen.io/anon/pen/jPrabY

share|improve this answer

You could write the CSS rule in JavaScript and add it to a stylesheet dynamically. A couple of good articles on how to do that are here and here.

var myColor = '#FF00FF';
var stylesheet = /* get stylesheet element */;
stylesheet.insertRule('.dynamic-color { background-color:"' + myColor +'";}',0);

Of course, in a pure Angular way, you would create a directive that wraps the DOM/stylesheet interaction.

share|improve this answer
    
hm, how it is better to get stylesheet in angular? maybe you have some code? –  brabertaser1992 yesterday
    
@brabertaser1992 There is some code for that at each of the two links. The simplest method would be to add an ID to your stylesheet and retrieve it using document.getElementById. –  Tibos yesterday

The easiest way I can think about is, for example, clicking on myBox changes its background-color.

html:

<div class="myBox" ng-click="changeBackgroundColor()"></div>

js:

$scope.changeBackgroundColor = function(){
  angular.element('.myBox').css('background-color', '#000');
}

css:

.myBox{background-color: #fff;}

Hope I've been helpfull.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.