【问题标题】:How to set background color using Angular?如何使用 Angular 设置背景颜色?
【发布时间】:2013-08-30 11:33:41
【问题描述】:

这应该是一件简单的事情,但由于某种原因它根本不起作用。

在我的控制器中:

$scope.projects = [ //...
    { background: "#ffffcc" }, 
    //... 
];

在 HTML 中:

<div ng-repeat="project in projects" 
     ng-style="{'backgroundColor': project.background}">
</div>

如果我将project.background 替换为'#ffffcc',它将起作用。我也尝试过background-color,但没有成功。还有{{project.background}}

【问题讨论】:

  • 工作正常here
  • 为什么不用指令,顺便说一下为什么要用内联css?
  • 你需要像project.background一样在范围内使用它

标签: javascript css angularjs


【解决方案1】:

你可以这样做

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

您也可以使用控制器中的属性,例如;

$scope.styles= [{
    backgroundColor: 'red',
    width:'100px',
    height: 'auto',
    display: 'block'}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2013-05-06
    • 2016-03-16
    • 2012-09-21
    • 2018-10-19
    相关资源
    最近更新 更多