El Dude
El Dude

Reputation: 5618

AngularJS div background color with ngStyle

One of my div s is observing a background color variable with ngStyle. Anytime I change the the value, I get an error. Please see below, I think I am more or less consistent with this example https://docs.angularjs.org/api/ng/directive/ngStyle

<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)

Upvotes: 1

Views: 731

Answers (1)

miensol
miensol

Reputation: 41638

The ng-style expects an object with styles i.e.:

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

You're passing a string instead of object hence the error.

Here's what documentation says about expected ng-style argument:

Expression which evals to an object whose keys are CSS style names and values are corresponding values for those CSS keys.

Upvotes: 1

Related Questions