样式元素中的AngularJS无法正常工作

时间:2012-06-06 00:23:20

标签: angularjs

我正在尝试在样式元素中使用角度js绑定,但它似乎不起作用。这是一个错误,还有更好的方法吗?

<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/

2 个答案:

答案 0 :(得分:7)

我不认为Angular会分析样式元素。您可能想要使用ngStyle指令:

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(像jsFiddle,但更像Angular友好)Demo

答案 1 :(得分:1)

现在可以在较新版本的Angular中使用。

这里http://jsfiddle.net/PpyVn/4/是用1.0替换为1.3.0-rc.3的OP小提琴。

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