I would like to apply a background-color
with ng-style
conditionally when the color value can be updated from $scope
.
当颜色值可以从$ scope更新时,我想有条件地应用具有ng风格的背景颜色。
Here are my variables in scope:
这是我的范围变量:
$scope.someone = { id: '1', name: 'John', color: '#42a1cd' };
$scope.mainId = 1;
Actually I don't use any condition to apply the background color, it is done with:
实际上我没有使用任何条件来应用背景颜色,它完成了:
<div ng-style="{'background-color': someone.color}">
{{someone.name}}
</div>
The idea here is to apply this background color only when someone.id == mainId
. I tried several solutions, but it does not seems to be the correct synthax:
这里的想法是仅在someone.id == mainId时应用此背景颜色。我尝试了几种解决方案,但它似乎不是正确的合成器:
ng-style="{{someone.id == mainId}} ? ('background-color': {{someone.color}}) : null"
ng-style="{ ('background-color': someone.color) : someone.id == mainId }"
ng-style =“{{someone.id == mainId}}?('background-color':{{someone.color}}):null”
ng-style =“{('background-color':someone.color):someone.id == mainId}”
JSFiddle for testing
Does someone have an idea of how I can achieve this?
有人知道如何实现这一目标吗?
3 个解决方案
#1
4
This seems to work fine.
这似乎工作正常。
ng-style="{'background-color': person.id === mainId ? person.color : ''}"
See updated fiddle with two people - the one with mainId
has a background color, while the other doesn't: http://jsfiddle.net/fgc21e86/7/
看到两个人的更新小提琴 - mainId有一个背景颜色,而另一个没有:http://jsfiddle.net/fgc21e86/7/
#2
3
You may try like this
你可以这样试试
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-style="{'background-color': getColor(someone)}">
{{someone.name}}
</div>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.someone = {
id: 1,
name: 'John',
color: '#42a1cd'
};
$scope.getColor = function(someone) {
if($scope.mainId === someone.id) {
return someone.color;
}
}
$scope.mainId = 1;
}]);
#3
2
You could do it like
你可以这样做
ng-style="someone.id == mainId ? {'background-color':'{{someone.color}}'} : {'background-color':'red'}"
Here's an updated fiddle http://jsfiddle.net/fgc21e86/9/
这是一个更新的小提琴http://jsfiddle.net/fgc21e86/9/
#1
4
This seems to work fine.
这似乎工作正常。
ng-style="{'background-color': person.id === mainId ? person.color : ''}"
See updated fiddle with two people - the one with mainId
has a background color, while the other doesn't: http://jsfiddle.net/fgc21e86/7/
看到两个人的更新小提琴 - mainId有一个背景颜色,而另一个没有:http://jsfiddle.net/fgc21e86/7/
#2
3
You may try like this
你可以这样试试
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-style="{'background-color': getColor(someone)}">
{{someone.name}}
</div>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.someone = {
id: 1,
name: 'John',
color: '#42a1cd'
};
$scope.getColor = function(someone) {
if($scope.mainId === someone.id) {
return someone.color;
}
}
$scope.mainId = 1;
}]);
#3
2
You could do it like
你可以这样做
ng-style="someone.id == mainId ? {'background-color':'{{someone.color}}'} : {'background-color':'red'}"
Here's an updated fiddle http://jsfiddle.net/fgc21e86/9/
这是一个更新的小提琴http://jsfiddle.net/fgc21e86/9/