gpt4 book ai didi

javascript - 将 ng-style 与 switch 语句一起使用 - Angular JS

转载 作者:行者123 更新时间:2023-12-02 15:05:20 25 4
gpt4 key购买 nike

是否可以将 ng-style 与返回非 bool 值的函数一起使用?

我想根据模型中的属性为不同的背景着色,并且我成功做到了

<tr ng-repeat="issue in data.issues|orderBy:orderByField:reverseSort" ng-style="{'background-color': isToday(issue.due_date) ?  'red': 'yellow'}" >
...

Controller :

    $scope.isToday = function (compareDate) {
var today = $scope.today;
return compareDate < today.getFullYear() + '-' + today.getDate() + '-' + today.getMonth();
}

其中 isToday 函数返回一个 bool 值。

如何处理我的函数返回 3 个值(或更多)并且我希望根据其结果拥有 3 种不同的背景颜色的情况?

最佳答案

使用ngClass。模板:

<tr ng-repeat="issue in data.issues|orderBy:orderByField:reverseSort" ng-class="{{ myClass }}" >

CSS:

.one {
background-color: red;
}

.two {
background-color: blue;
}

.three {
background-color: green;
}

js:

$scope.myClass = 'three' // or 'one', or 'two'

关于javascript - 将 ng-style 与 switch 语句一起使用 - Angular JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35151993/

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