2013-08-30 15 views
0

這應該是一件簡單的事情,但由於某種原因,它根本不起作用。如何使用Angular設置背景顏色?

在我的控制器:

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

在HTML:

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

如果我SUB OUT project.background'#ffffcc'它會奏效。我也試過background-color沒有成功。除了{{project.background}}

+2

做工精細[這裏](http://plnkr.co/edit/6NfwZ8KCxk9nnH014FpU?p=preview) –

+1

爲什麼不使用指令,爲什麼您使用內聯CSS的方式? – kangoroo

+0

你需要使用它像project.background,在範圍內 – kangoroo

回答

0

你可以做這樣的事情

<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'}];