2014-01-10 140 views
2

我想要动态注入一个div的背景图像。我看到图片获取加载正常,但CSS不会改变,div从不会获得背景图像。我究竟做错了角度不工作的css

我有以下代码:

<style> 
#container { 
width:100%; 
} 
.left_column { 
float:left; 
width:37%; 
height: 150px; 
/*background-color: red;*/ 
} 
.right_column { 
float:right; 
width: 37%; 
height: 150px; 
} 
.snipe{ 
    float: right; 
    background-color: red; 
    color: white; 
    position: relative; 
    margin-top: 99%; 
} 
</style> 
<div ng-app="myApp"> 

    <div ng-controller="ShowCtrl"> 
     <div class="content-padded"> 
      <h2>{{zucrew.name}}</h2> 
     </div> 
     <div class="topcoat-list__item" hm-tap="open(person.id)" 
       ng-repeat="person in persons" ng-class-odd="'left_column'" ng-class-even="'right_column'" 
       ng-style="{'background-image' : 'url({{person.src}})'}"> 
      {{ person.name }} 

     </div> 

    </div> 

</div> 

回答

3

,因为路ngStyle希望quoted-,而不是使用双curlys使用加号工作性质:

<div ng-repeat="person in persons" ng-style="{'background-image': 'url(' + person.src + ')'}"> 

demo fiddle

+0

宾果!像魅力一样工作 – Autolycus

2

你不应该需要在大括号括person.src,因为你已经将被计算的表达式中工作ngStyle指令。试试这个:

<div class="topcoat-list__item" hm-tap="open(person.id)" 
      ng-repeat="person in persons" ng-class-odd="'left_column'" ng-class- even="'right_column'" 
      ng-style="{'background-image' : 'url(person.src)'}"> 
     {{ person.name }} 

    </div> 

请参阅http://plnkr.co/edit/qtVH5IuPD44L43D2xHIZ?p=preview了解相似,更简单的示例。

+0

嗯没有工作对我来说:( – Autolycus

+0

这里是有趣的事情...当我硬编码它像这样ng-style =“{'background-image':'url(http://www.babybedding.com/blog/wp-content/uploads/2008/07/sockmonkey .jpg)'}“它的作品,但当我这样做ng-style =”{'background-image':'url(person.src)'}“它不起作用:(而我console.logged peron.src和它是同一个地址... – Autolycus

+0

我们很近:)。 @KayakDave已经明白了。 –