1

I want to make an application with dynamic theming. For example, there is a button, you click it and color of some elements changes by some rule. First idea - do it with

<style type="text/css" ng-bind="ownStyle"></style>

and init ownStyle in $rootScope:

$rootScope.ownStyle = "* {color: green }";

But it seems awful + it's too hard to write css rules as string. Is there a more elegancy way to do it?

1
  • It could be possible to simply have the rules in your own CSS file, but preface them all with a particular class (eg .ownStyle .myThing). Then, to turn it on, add that class to a containing element. Commented Aug 15, 2016 at 13:29

3 Answers 3

3

Can try something like this..

$rootScope.color = red;

$scope.changecolor = function(){
  $rootScope.color = blue;  
}
/* Base color */
.dinamic{
  background-color: white;  
}
<style>
  .dinamic{
    background-color: {{$rootScope.color}} !important;
  }
</style>

<button class="dinamic">I Will Change Color</button>

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this solution is better!
1

You can use ng-class directive.

Just add this directive to "some elements", set proper condition and it'll be fine.

Comments

1

You can use ng-class built in directive.

<div ng-class="{'some-class': condition}></div>

Comments

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.