2016-01-15 33 views
1

我的div中的一個是用ngStyle觀察背景顏色變量。任何時候我改變這個值,我會得到一個錯誤。下面請看,我覺得我有這個例子https://docs.angularjs.org/api/ng/directive/ngStylengStyle的AngularJS div背景顏色

<div class="item item-divider" ng-style="scoreColor"><b>Rate</b>&nbsp;{{score}}</div> 
<input type="range" name="rate" min="1" max="10" value="5" step="1" ng-model="score" ng-change="changeScoreColor(score)"/> 

$scope.changeScoreColor = function(score){ 
    $scope.scoreColor = "{background-color:" + colorFromScore(score) + ";}"; 
}; 



TypeError: name.replace is not a function 
    at camelCase (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11098:5) 
    at forEach.css (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11541:12) 
    at Object.JQLite.(anonymous function) [as css] (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11667:9) 
    at http://localhost:8100/lib/ionic/js/ionic.bundle.js:33610:57 
    at forEach (http://localhost:8100/lib/ionic/js/ionic.bundle.js:9022:20) 
    at ngStyleWatchAction (http://localhost:8100/lib/ionic/js/ionic.bundle.js:33610:7) 
    at Object.$watchCollectionAction [as fn] (http://localhost:8100/lib/ionic/js/ionic.bundle.js:22809:13) 
    at Scope.$digest (http://localhost:8100/lib/ionic/js/ionic.bundle.js:22942:29) 
    at Scope.$apply (http://localhost:8100/lib/ionic/js/ionic.bundle.js:23205:24) 
    at $$debounceViewValueCommit (http://localhost:8100/lib/ionic/js/ionic.bundle.js:31961:14) 

回答

1

ng-style或多或少一致期待與樣式的對象,即:

$scope.changeScoreColor = function(score){ 
    $scope.scoreColor = {'background-color': colorFromScore(score) }; 
}; 

你傳遞一個字符串,而不是對象因此錯誤。

以下是文件說,關於預期ng-style的說法:這evals一個對象,它的鍵是CSS樣式名稱和 值對應的那些CSS鍵的值

表達。

+0

工程,太棒了!非常感謝! –