2016-11-23 24 views
0

我有這個網站在我看來

<div ng-repeat="newsItem in Items" > 
    <div style="background: url({{::newsItem.teaser) no-repeat center center; background-size: cover;"></div> 
</div> 
$rootScope我有一個包含這種方式產生的本地URL對象

  • $ rootScope.images.someName
  • $ rootScope。 images.someNameX
  • $ rootScope.images.someName3

我的變量newsItem.teaser包含名稱someName,someNameX,someName3。我如何在我的視圖中使用$ root ['someName']?這可能嗎?

回答

0

的解決方案是這樣

<div ng-style="{'background': 'url({{$root.images[newsItem.teaser]}}) no-repeat center center', 'background-size': cover}"> </div> 
2

不需要在html中指定$ rootScope。當您使用$範圍內的變量可以用同樣的方式

<h1>{{newsItem.teaser.someName}}</h1> 
+0

使用ngStyle當我剛剛離開它這樣,我成爲了這個'..網址(someName)no-repeat ...'它只是使用文本,而不是尋找'$ rootscope'項目 – distante

+0

@distante我沒有得到它 – Sajeetharan

+0

@Sabeetharan它只是將該名稱呈現爲一個字符串。 – distante