在我的 Angular 应用程序中,如果日期等于今天,我需要将表格行的背景色设为红色。
直到现在,我只有在等于“2016-01-31”时才设法设置红色,
如果“2016-01-31”是当前日期,我该如何让它工作?
<tbody>
<tr ng-repeat="issue in data.issues|orderBy:orderByField:reverseSort" ng-style="{'background-color': issue.due_date == '2016-01-31' ? 'red': 'white'}" ng-click="setSelected()">
<td>{{issue.id}}</td>
<td>{{issue.project.name}}</td>
<td>{{issue.subject}}</td>
<td>{{issue.start_date}}</td>
<td>{{issue.due_date}}</td>
<td>{{issue.priority.name}}</td>
</tr>
</tbody>
最佳答案
使用一个函数来定义你的值是否是今天
<tbody ng-controller="MyCtrl">
<tr ng-repeat="issue in data.issues|orderBy:orderByField:reverseSort" ng-style="{'background-color': isToday(issue.due_date) ? 'red': 'white'}" ng-click="setSelected()">
<td>{{issue.id}}</td>
<td>{{issue.project.name}}</td>
<td>{{issue.subject}}</td>
<td>{{issue.start_date}}</td>
<td>{{issue.due_date}}</td>
<td>{{issue.priority.name}}</td>
</tr>
</tbody>
并在您的应用中定义此功能
.controller('MyCtrl', function($scope){
$scope.today = new Date();
$scope.isToday = function(compareDate){
var today = $scope.today;
return compareDate == today.getFullYear() + '-' + today.getDate() + '-' + today.getMonth();
}
})
我还建议使用类而不是内联样式,请参阅 ng-class
了解更多信息
关于javascript - 以 ng-style 获取当前日期-Angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35135375/