在ng repeat中按条件设置格式/样式
Conditionally format/style inside ng-repeat
我正试图想出一种方法,用AngularJS控制器的结果有条件地格式化html表(简单的背景色)。这将有望在ng repeat的迭代过程中发生,因为它在数组中的元素中工作。
我想实现的是:每当控制器内的函数返回true时,背景或样式都是绿色的,而当不是时,它将是红色的
在插入点的注释中查找"开始问题"。
<!-- PLAYER STATS TABLE -->
<div id = "StatsListTable">
<table border="1" style="width:80%">
<tr> <!-- TODO: class="tr.pstats" -->
<th>Character Name</th>
<th>
<div ng-app="KIdash" ng-controller="controller">
Total Matches: {{TotalMatchesFunc()}} :: {{GreaterWins()}}
</div>
</th>
<th class="green">Wins</th>
<th class="red">Losses</th>
</tr>
<tr ng-repeat="ps in PlayerStats" class = "playerStatTable">
<td>{{ps.name}}</td>
<td>{{ps.matches}}</td>
<!-- BEGIN QUESTION -->
<!-- IF {{GreaterWins()}} make it green -->
<!-- ELSE make it red -->
<script type="text/javascript">
function() {
if( controller.GreaterWins() )
{
<th class="green">{{ps.wins}}</th>
} else {
<th class="red">{{ps.wins}}</th>
}
}
</script>
<!-- END IF -->
<td>{{ps.losses}}</td>
</tr>
<table>
</div>
<!-- END PLAYER STATS TABLE -->
您可以通过ng类来实现这一点。如下所示:
<tr ng-repeat="ps in PlayerStats" class = "playerStatTable">
<td>{{ps.name}}</td>
<td>{{ps.matches}}</td>
<td ng-class="{'green': GreaterWins(),'red': !GreaterWins() }">{{ps.wins}}</td>
<td>{{ps.losses}}</td>
</tr>
相关文章:
- 使用正则表达式将输入格式设置为单字符逗号、单字符逗号等
- 将数字四舍五入到小数点后两位,以设置货币格式
- 在达到状态之前在Redux Reducer中设置日期格式
- 在不使用javascript的情况下,可以在表单字段中设置文本占位符(以提示最终用户插入特定格式)
- Angular指令中的最佳实践是将文本输入设置为英尺和英寸的格式
- 使用不同的日期格式和区域设置创建时刻
- Mustache.js lambdas和数字格式设置为Fixed
- 如何从区域设置获取日期格式
- 如何在我的情况下设置日期格式
- 更改默认时区后,如何在本地时间设置时刻格式
- 如何在时间标签和相同的类中以UTC格式重新格式化ISO 8601时间戳,并通过JavaScript根据用户区域设置和时区
- 为数据库存储设置角度日期格式
- 在ng repeat中按条件设置格式/样式
- Date.toLocaleString 使用区域设置格式但不调整时区
- d3.js.以区域设置格式显示数据集编号
- 如何在“;数据:图像”;在另一个选项卡中设置格式
- 如何在2.10版本中覆盖moment.js区域设置格式.X,在2.8.x中运行
- 如何使用角矩来设置格式?
- 文本字段中的数字应仅在用户单击文本框外后自动设置格式
- 谷歌图表双轴,设置格式