这应该是一件简单的事情,但由于某种原因,它根本不起作用。
在我的 Controller 中:
$scope.projects = [ //...
{ background: "#ffffcc" },
//...
];
在 HTML 中:
<div ng-repeat="project in projects"
ng-style="{'backgroundColor': project.background}">
</div>
如果我用 '#ffffcc'
分出 project.background
它将起作用。我也尝试过 background-color
但没有成功。以及 {{project.background}}
最佳答案
你可以这样做
<div ng-init="styles=[{backgroundColor: 'red', width:'100px', height: 'auto', 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>
您还可以在 Controller 中使用属性,例如;
$scope.styles= [{
backgroundColor: 'red',
width:'100px',
height: 'auto',
display: 'block'}];
关于javascript - 如何使用 Angular 设置背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18524457/