2014-01-10 137 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已經明白了。 –