gpt4 book ai didi

javascript - 根据多种条件处理不同颜色的文本

转载 作者:太空宇宙 更新时间:2023-11-04 11:07:36 24 4
gpt4 key购买 nike

此代码使用 angularjs ng-table 模块在表格中显示值。

相关的html代码如下所示;

<div ng-controller="ViewCtrl" class="container">
<table ng-table="tableParams" class="table table-bordered">
<thead>

<tr>
<th>price_alert</th>
<th>lastPrice</th>
</tr>

<thead>
<tbody>
<tr ng-repeat="item in $data">
<td data-title="'price_alert'" ng-class="{ red: item.lastPrice < stk.price_alert }>
${{item.price_alert}}
</td>
<td data-title="'lastPrice'" ng-class="{ red: item.lastPrice < stk.price_alert }>
${{item.lastPrice}}
</td>
</tr>
</tbody>
</tbody>
</table>
</div>

CSS代码;

.red { color: red; }

Controller 代码;

controller('ViewCtrl', ['$scope', '$http', 'moment', 'ngTableParams',
function ($scope, $http, $timeout, $window, $configuration, $moment, ngTableParams) {
var tableData = [];
//Table configuration
$scope.tableParams = new ngTableParams({
page: 1,
count: 100
},{
total:tableData.length,
//Returns the data for rendering
getData : function($defer,params){
var url = 'http://127.0.0.1/list';
$http.get(url).then(function(response) {
tableData = response.data;
$defer.resolve(tableData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
params.total(tableData.length);
});
}
});
}])

item.lastPrice < item.price_alert时,文字颜色会变成红色.如果我希望在 item.lastPrice < item.price_alert 时文本颜色变为红色怎么办?并在 item.lastPrice > item.price_alert 时变为绿色?可以更改代码以处理多种情况吗?类似于使用 switch 语句?

最佳答案

创建一个新的 css 类:

.red { color: red; }
.green { color: green; }

并根据以下内容更改您的 ng-class 标签:

<td data-title="'price_alert'" ng-class="{ red: item.lastPrice < stk.price_alert, green: item.lastPrice > item.price_alert }> /*....*/

关于javascript - 根据多种条件处理不同颜色的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com