admin管理员组文章数量:1406344
I have this table showing software logs, which gets populated by JSON data:
<div ng-controller="LogCtrl">
<table id="logtable" style="width:100%">
<tr>
<th>Timestamp</th>
<th>Severity</th>
<th>Message</th>
</tr>
<tr ng-repeat="log in logs" class="logentry">
<td>{{log.timestamp}}</td>
<td>{{log.severity}}</td>
<td>{{log.message}}</td>
</tr>
</table>
It works fine, but I would like to be able to change each tr
element background following its severity. For example, if the severity of that log is "ERROR", its entry in the table should have red background, if the severity is "MESSAGE", it should be green, etc.
I already took a look at ng-style
, but I didn't find out how to use it for this purpose.
Any suggestions?
I have this table showing software logs, which gets populated by JSON data:
<div ng-controller="LogCtrl">
<table id="logtable" style="width:100%">
<tr>
<th>Timestamp</th>
<th>Severity</th>
<th>Message</th>
</tr>
<tr ng-repeat="log in logs" class="logentry">
<td>{{log.timestamp}}</td>
<td>{{log.severity}}</td>
<td>{{log.message}}</td>
</tr>
</table>
It works fine, but I would like to be able to change each tr
element background following its severity. For example, if the severity of that log is "ERROR", its entry in the table should have red background, if the severity is "MESSAGE", it should be green, etc.
I already took a look at ng-style
, but I didn't find out how to use it for this purpose.
Any suggestions?
Share Improve this question edited May 17, 2015 at 16:06 prtnkl asked May 17, 2015 at 15:20 prtnklprtnkl 3021 gold badge4 silver badges12 bronze badges2 Answers
Reset to default 3You can achieve this by ng-class
conditional operator
<tr ng-repeat="log in logs" class="logentry"
ng-class="{ 'ERROR': 'severity-error', 'MESSAGE': 'severity-message'}[log.severity]">
<td>{{log.timestamp}}</td>
<td>{{log.severity}}</td>
<td>{{log.message}}</td>
</tr>
CSS
.severity-error {
background-color: red;
}
.severity-message {
backgroud-color: green;
}
Same as above, but with ng-style
.
<tr ng-repeat="log in logs"
ng-style="{'ERROR':{background:'red'}, 'INFO':{background: 'green'}}[log.severity]">
<td>{{log.timestamp}}</td>
<td>{{log.severity}}</td>
<td>{{log.message}}</td>
</tr>
var app = angular.module('app', []);
app.controller('tableController', function($scope) {
$scope.logs = [
{
timestamp: 'foo',
severity: 'ERROR',
message: 'Something bad happened'
},
{
timestamp: 'bar',
severity: 'INFO',
message: 'This is ok'
},
];
});
<script src="https://code.angularjs/1.4.0-rc.2/angular.js"></script>
<table ng-controller="tableController" ng-app="app">
<tr ng-repeat="log in logs"
ng-style="{'ERROR':{background:'red'}, 'INFO':{background: 'green'}}[log.severity]">
<td>{{log.timestamp}}</td>
<td>{{log.severity}}</td>
<td>{{log.message}}</td>
</tr>
</table>
本文标签: javascriptAngularJS setting background color dynamicallyStack Overflow
版权声明:本文标题:javascript - AngularJS: setting background color dynamically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745004846a2637199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论