2

Is it possible to dynamically change the value of a CSS property of a class using Angular? I am trying to pass a color value dynamically to CSS after it is retrieved from database, and I am in search of a solution for this.

I have been trying to see if I could make ng-animate work for this, but it is not as straightforward.

Any help is appreciated.

1
  • Why passing a value to CSS? Can't you just add another class based on an event or something? Commented Jun 14, 2016 at 2:55

6 Answers 6

1

Looks like following link has answer to you question. I would like to call it javascript way instead of angular way

How to dynamically create CSS class in JavaScript and apply?

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

Comments

1

There are multiple options, one of the ways is using ng-style. Let's say you have colors coming for each elements.

<ul>
  <li ng-repeat="item in items" ng-style="{'color': item.color}"></li>    
</ul>

If you want a single style color to be applied to all items of a class.

angular.element('.className').css('color','#DBCLOR');

1 Comment

This would work. However, I have a further complication in that I have a CSS file that implements some animation on these colors. The animation needs the value of the color in several classes. Is there a way to do that efficiently?
1

If you know the class name which is already defined somewhere (in stylesheet, html) you can again create style and give new values to the class. The new values will be applied to all the elements which are having this class. Try to create a new style tag and create the property and value for it using javascript. Try to append this into head. If this does not work try to append this to body.

var style = document.createElement('style');
var newColor = '#efefef'; //fetched from AJAX or anywhere
style.type = 'text/css';
style.innerHTML = '.cssClass { color: '+ newColor +  '; }';
document.getElementsByTagName('head')[0].appendChild(style);

Comments

0

Use ng-style to accomplish this

var jimApp = angular.module("mainApp",  []);

jimApp.controller('mainCtrl', function($scope){
  $scope.colors = ['red', 'blue', 'green'];
});
.item{
  padding:5px;
  width:100px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
  <div class="item" ng-repeat="color in colors" ng-style="{'background-color':color}">{{color}}</div>
</div>

4 Comments

How is your answer different then mine ?
@Don Your code does not work, ng-style="{'color': item.color}" add color attribute in single quotes
Its meant to be a guide point, rather than actual code. User has not shared any code snippet anyway. And even if you find small issues like single quotes, shouldn't it be a comment to my answer rather than creating a duplicate answer ?
This would work. However, I have a further complication in that I have a CSS file that implements some animation on these colors. The animation needs the value of the color in several classes. Is there a way to do that efficiently?
0

if you dynamically change the value of a CSS property of a class, you would refer to ng-class

  1. using ng-class

    <ANY ng-class="expression"> ... </ANY>

    you also can do as below, if expression is true, it will add class 'class-a'

only have a class:

<div ng-class="{ class-a: expression }"></div>

more than two classes:

<div ng-class="{ class-a: expressionA, class-b: expressionB, class-c: expressionC }"></div>
  1. using $scope

    html:

    <div class="{{classPro}}></div>

    js:

    $scope.classPro = "className";

3 Comments

I am not looking to add a class. I am looking to change the value of a field on an existing class. Is that possible with ng-class?
yes, you can do it! if you want to change the value of calss, you can use <div ng-class="{ class-a: expressionA, class-b: expressionB }"></div> , when expressionA is false and expressionB is true, it means change class-a to class-b
Once again, you are misunderstanding what I am trying to do. I am asking if it is possible to have Class A with, say, {color: #ffffff} and Class B with {color: #aaaaaa}. Now, I am asking if I can dynamically assign these hex values for any class using Angular.
0

In your css file:

.classA {
  color: var(--your-color);
}

Your html (angular)

<p class="classA" [style.--your-color]="data.color"> YOUR CONTENT </p>

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.