How to change background color using ng-style?
this div will be repeated so that one of the colors is from the database. For plnkr, I just set the colors, but in my example it is like this:
<div class="col-md-offset-0 col-md-2" ng-repeat="type in types" style="margin-bottom:5px;"> <div class='container' divCheckbox ng-style="{'background-color':(isSelected?'{{type.color}}':'#ccc')}> <input type='hidden' ng-model="type.show" /> <div class="label">{{type.name}}</div> </div> </div>
And the directive:
.directive('divCheckbox', function () { return { restrict: 'A', link: function (scope, el, attr) { scope.isSelected = el.find('input').val() == 'false'; el.on('click', function () { scope.isSelected = !scope.isSelected; scope.$apply(); }); } } });
Heres my plnkr: http://plnkr.co/edit/onLA8vSbtwQu1OxZrKzT?p=preview
source share