2015-09-09 30 views
0

我的角度網站從Web API後端獲取數據,最初顯示一般背景。但是登錄後,客戶背景圖像url被檢索並用於我的ng樣式屬性。Angular ng風格增加了一個風格屬性,然後不保持同步

當頁面初始加載時,還會出現一個不屬於源代碼的樣式屬性,該屬性將背景指向默認圖像。

但是,登錄後,即使ng樣式現在顯示正確的背景圖像url,style屬性仍然指向默認的url。

如果我手動刷新頁面(Chrome中的開發人員設置中沒有緩存),則更新style屬性並正確顯示客戶背景。

發生了什麼事?爲什麼會角色添加一個樣式屬性,然後不保持同步?

源代碼

<div class="background-div" ng-style="{'background-image': 'url({{user.profile.Customer.BackgroundImageUrl || '../images/bg.jpg'}})'}"></div> 

初始頁面加載

<div class="background-div" ng-style="{'background-image': 'url(../images/bg.jpg)'}" style="background-image: url(http://localhost:10223/images/bg.jpg);"></div> 

後登錄

<div class="background-div" ng-style="{'background-image': 'url(http://megacorp.com/images/vipcustomer.jpg)'}" style="background-image: url(http://localhost:10223/images/bg.jpg);"></div> 

被迫無緩存刷新

<div class="background-div" ng-style="{'background-image': 'url(http://megacorp.com/images/vipcustomer.jpg)'}" style="background-image: url(http://megacorp.com/images/vipcustomer.jpg);"></div> 

固定碼

<div class="background-div" 
     ng-style="{'background-image': 'url(' + (user.profile.Customer.BackgroundImageUrl || '../images/bg.jpg') + ')'}"> 
    </div> 

重新打開後:

恕我直言,這個問題是不同的B/C它停留在HTML和不進入$摘要和腳本,並顯示頁面處於不同狀態的階段。但我依照你的判斷:)

回答

1

ng-style不應該包含{{}}插值,你可以在它裏面使用直接範圍變量。

標記

<div class="background-div" 
    ng-style="{'background-image': 'url('+user.profile.Customer.BackgroundImageUrl || 
    '../images/bg.jpg'+')'}"> 
</div> 
+0

我不得不把括號周圍的表達,因此將在連接前做OR。然後它工作。看到我的編輯顯示修復。謝謝! – toddmo

+0

@toddmo不會在'ng樣式中使用插值括號' –

+0

我的意思是我必須放置'('和')',而不是'{{'和'}}'。謝謝! – toddmo