5

我正在尝试在样式元素中使用角度 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/

4

2 回答 2

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 更友好)演示

于 2012-06-06T01:06:06.150 回答
1

这现在适用于较新版本的 Angular。

这里http://jsfiddle.net/PpyVn/4/是 OPs 将 1.0 替换为 1.3.0-rc.3。

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.3/angular.min.js"></script>
于 2014-09-29T04:16:07.027 回答