gpt4 book ai didi

javascript - 具有不同条件的angularjs ng-class

转载 作者:搜寻专家 更新时间:2023-11-01 04:46:36 24 4
gpt4 key购买 nike

我有一个显示数据的 View ,我想在我的 View 中的列表项上添加另一个类。

<input type="text" class="filter" placeholder="search..." ng-model="search">
<ul>
<li ng-repeat="item in items | filter:search | orderBy:'date'">
{{ date }} {{ item.heading }}<button ng-click="deleteItem(item.ID)"><i class='icon-trash'></i></button>
</li>
</ul>

我有一个名为 item.date 的变量,我想将它与 今天 的另一个变量进行比较。这是逻辑:

if (item.date - today <= 0) 
apply class1
if else (item.date - today > 0 && item.date - today <= 3)
apply class2
and so on

我怎样才能用angular实现这个?我可以将此逻辑直接放入我的 View 中还是需要在我的 Controller 中定义它?

提前致谢

最佳答案

由于您要进行比较繁重的比较,我建议将其移入函数中而不是将其放在 HTML 中:

<li ng-class="getClass(item.date)" 
ng-repeat="item in items | filter:search | orderBy:'date'">

JS:

$scope.getClass = function(date){
/* comparison logic here*/
/* returs class name as string */
}

关于javascript - 具有不同条件的angularjs ng-class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18208211/

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