Angularjs 尝试有条件地使用ng样式
下面的代码不会更改表的任何颜色。行上看不到红色或绿色(正确渲染的不同标签除外) 有什么想法吗Angularjs 尝试有条件地使用ng样式,angularjs,Angularjs,下面的代码不会更改表的任何颜色。行上看不到红色或绿色(正确渲染的不同标签除外) 有什么想法吗 <table class="table table-striped table-hover"> <thead> <tr> <th>Player</th> <th>Number</th> <th>Still stan
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Player</th>
<th>Number</th>
<th>Still standing?</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="player in players" ng-style="player.standing ? '{'color':'green'}' : {'color':'red'}">
<td ng-cloak>
<span ng-show="player.standing">
{{player.name}}
</span>
<span ng-show="!player.standing">
<strike>{{player.name}}</strike>
</span>
</td>
<td ng-cloak>{{ player.associatedNumber }}</td>
<td ng-cloak>
<span
ng-class="player.standing ? 'label label-success': 'label label-danger'"
ng-show="player.standing">
Yes
</span>
<span
ng-class="player.standing ? 'label label-success': 'label label-danger'"
ng-show="!player.standing">
No
</span>
</td>
</tr>
</tbody>
</table>
您的表达式没有为
ng style
生成有效的结果。你想要
ng-style="{color: player.standing ? 'green' : 'red'}"
您还可以使用相应的标签,以便这些标签与其他代码中的标签对齐
<tr ng-repeat="player in players" ng-class="{
'text-success': player.standing,
'text-danger': !player.standing}">
而不是具有冗余逻辑的两个跨度
<tr ng-repeat="player in players" ng-class="{
'text-success': player.standing,
'text-danger': !player.standing}">
<span class="label" ng-class="{
'label-success': player.standing,
'label-danger': !player.standing
}">{{player.standing ? 'Yes' : 'No'}}</span>