javascript - 如何使用 Angular 设置背景颜色?

标签 javascript css angularjs

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

在我的 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/

相关文章:

javascript - 使用 innerHTML 防止脚本注入(inject)

javascript - 如果我没有全局安装 selenium-webdriver,我该如何更新它?

javascript - 使用父类(super class)的子类中定义的静态属性

html - 如何为 Google Chrome 和 Google Tasks 网站创建用户样式?

javascript - 在生产模式下运行时,yeoman 应用程序中不显示图像

javascript - 如何为特定节点编写条件 xpath 选择?

html - 只使用 html/CSS 在不知道列数的情况下使表中的列相等

javascript - 查找数组中的属性并获取索引

javascript - 使用 jQuery 设置值时 ng-model 不起作用

css - 在线响应式 Web 测试仪