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

cms*_*che 5 angularjs

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

<style ng-repeat="test in styles">
.test {
    background-color: {{ test.backgroundColor }};
    display: {{ test.display }};
    width: {{ test.width }};
    height: {{ test.height }};
    }
</style>
Run Code Online (Sandbox Code Playgroud)

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

btf*_*ord 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>
Run Code Online (Sandbox Code Playgroud)

Plunker(比如jsFiddle,但更加Angular友好)Demo

  • 我想在样式标签中找到一种方法,以便给'a:hover`一些动态样式......任何想法? (2认同)