Is there a way to set a background-color
conditionally using ng-style
when the color value can be updated from $scope
?
These are the variables in my scope:
$scope.someone = { id: '1', name: 'John', color: '#42a1cd' };
$scope.mainId = 1;
Currently, I am setting the background color without any conditions like this:
<div ng-style="{'background-color': someone.color}">
{{someone.name}}
</div>
I want to apply this background color only when someone.id == mainId
. I have tried multiple solutions but none of them seem to be correct:
ng-style="{{someone.id == mainId}} ? ('background-color': {{someone.color}}) : null"
ng-style="{ ('background-color': someone.color) : someone.id == mainId }"
Check out JSFiddle demo for testing
If anyone has an idea on how to achieve this, please let me know!