Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply CSS style attribute dynamically in Angular JS

This should be a simple problem, but I can't seem to find a solution.

I have the following markup:

<div style="width:20px; height:20px; margin-top:10px; border:solid 1px black; background-color:#ff0000;"></div> 

I need the background color to be bound to the scope, so I tried this:

<div style="{width:20px; height:20px; margin-top:10px; border:solid 1px black; background-color:{{data.backgroundCol}};}"></div> 

That didn't work, so I did some research and found ng-style, but that didn't work, so I tried taking the dynamic part out and just hard-coding the style in ng-style, like this...

<div ng-style="{width:20px; height:20px; margin-top:10px; border:solid 1px black; background-color:#ff0000;}"></div> 

and that doesn't even work. Am I misunderstanding how ng-style works? Is there a way of putting {{data.backgroundCol}} into a plain style attribute and getting it to insert the value?

like image 954
jonhobbs Avatar asked Jan 26 '14 14:01

jonhobbs


People also ask

How do I change dynamic style in CSS?

color = "red"; you can apply the style change dynamically. Below is a function that turns an element's colour to red when you pass it the element's id . You could also use setAttribute(key, value) to set a style on an element. For example, you could set the colour of an element to red by calling element.


1 Answers

ngStyle directive allows you to set CSS style on an HTML element dynamically.

Expression which evals to an object whose keys are CSS style names and values are corresponding values for those CSS keys. Since some CSS style names are not valid keys for an object, they must be quoted.

ng-style="{color: myColor}"

Your code will be:

<div ng-style="{'width':'20px', 'height':'20px', 'margin-top':'10px', 'border':'solid 1px black', 'background-color':'#ff0000'}"></div> 

If you want to use scope variables:

<div ng-style="{'background-color': data.backgroundCol}"></div> 

Here an example on fiddle that use ngStyle, and below the code with the running snippet:

angular.module('myApp', [])  .controller('MyCtrl', function($scope) {    $scope.items = [{        name: 'Misko',        title: 'Angular creator'      }, {        name: 'Igor',        title: 'Meetup master'      }, {        name: 'Vojta',        title: 'All-around superhero'      }      ];  });
.pending-delete {    background-color: pink  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <div ng-app="myApp" ng-controller='MyCtrl' ng-style="{color: myColor}">      <input type="text" ng-model="myColor" placeholder="enter a color name">      <div ng-repeat="item in items" ng-class="{'pending-delete': item.checked}">      name: {{item.name}}, {{item.title}}      <input type="checkbox" ng-model="item.checked" />      <span ng-show="item.checked"/><span>(will be deleted)</span>    </div>    <p>      <div ng-hide="myColor== 'red'">I will hide if the color is set to 'red'.</div>  </div>
like image 125
StarsSky Avatar answered Oct 20 '22 21:10

StarsSky