AngularJS inside style element not working

I am trying to use angular js bindings inside a style element, but it does not seem to work. This is a mistake, and is there a better way to do this?

<style ng-repeat="test in styles">
.test {
    background-color: {{ test.backgroundColor }};
    display: {{ test.display }};
    width: {{ test.width }};
    height: {{ test.height }};
    }
</style>

http://jsfiddle.net/cmsanche/PpyVn/

+5
source share
2 answers

I don't think Angular parses style elements. You probably want to use the ngStyle directive:

http://docs.angularjs.org/api/angular.module.ng.$compileProvider.directive.ngStyle

<!doctype html>
<html ng-app>
<head>
<script src="http://code.angularjs.org/angular-1.0.0rc10.min.js"></script>
</head>
<body>
<div ng-init="styles=[{backgroundColor: 'red', width:'100px', height: '100px', display: 'block'}]">

<div ng-repeat="test in styles" ng-style="test">
.test {
    background-color: {{ test.backgroundColor }};
    display: {{ test.display }};
    width: {{ test.width }};
    height: {{ test.height }};
    }
</div>
</html>

Plunker (e.g. jsFiddle, but more Angular-friendly) Demo

+7
source

This now works in newer versions of Angular.

Here http://jsfiddle.net/PpyVn/4/ is an OPs script replacing 1.0 with 1.3.0-rc.3.

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.3/angular.min.js"></script>
+1
source

All Articles