2016-09-14 26 views
2

我需要通過ng-style設置從配置對象元素的背景,但我不能這樣做,對於一些未知的原因,這對我來說真的很奇怪,我真的找不到理由。吳式不工作的背景

元素我想配置:

<div id="progress-l" ng-style="{ 'width': pblc.options.width, 'height': pblc.options.height, 'background': plbc.options.color }"></div> 

pblc代表控制器在這裏,我使用controllerAs選項

配置對象:

this.progressLOptions = { 
    color: '#F0F3F4', 
    width: '200px', 
    height: '20px', 
}; 

最奇怪的是寬度和高度設置,在呈現html我看到這個:

<div id="progress-l" ng-style="{ 'width': pblc.options.width, 'height': pblc.options.height, 'background': plbc.options.color }" style="width: 200px; height: 20px;"> 
</div> 

我真的不知道如何可以ng樣式工作是這樣的。有沒有我無法找到的背景問題?

+0

什麼是'pblc.options',正如我可以看到你有'progressLOptions',你有沒有搞砸範圍變量名? –

+0

@PankajParkar nope,我只是使用'controllerAs'作爲指令,如果是命名,高度和寬度不會設置得太 – kemsbe

回答

4

更新答案: 你有一個錯字 - 的plbc.options.color代替pblc.options.color

最可能的原因是,plbc.options.color是不確定的,空的,而不是在範圍或配置錯誤。這是在沒有語法錯誤的情況下。

嘗試輸出其作爲模板的表達 - {{plbc.options.color}},並檢查它。

5

使用'background-color'代替'background'

+0

謝謝,但是還是沒有運氣( – kemsbe

+0

順便說一句,你的顏色幾乎是白色的 – nubinub

+1

背景也有效在這種情況下沒有必要的背景色 – KrishCdbry

3

我已經改變了你的代碼了一下,請看看這個

<div id="progress-l" ng-style="{ 'width': progressLOptions.width, 'height': progressLOptions.height, 'background': progressLOptions.color }" style="width: 200px; height: 20px;"> 
    </div> 

JS

$scope.progressLOptions = { 
    color: '#000', 
    width: '200px', 
    height: '20px', 
    }; 

這是工作。如果你願意,你this.progressLoptions然後在HTML中使用使用$範圍,而不是這個或其他

<body ng-controller="somename as controllerName"> 
and then access this using somename.functionName 

演示:https://jsbin.com/zihazi/edit?html,js,output

+0

謝謝你的回答,這是正確的,但事實證明,我有一個錯字 – kemsbe

+0

Thatz爽!這就是爲什麼我做演示的是你的邏輯是正確的,但在某個地方有一個錯誤..!無論如何解決atlast。乾杯! – KrishCdbry