简体   繁体   English

如何在到期日更改字体颜色?

[英]How do I change the font color upon the expiration date?

I want set color when the expiration date has not yet been reached green.我想在到期日期尚未达到绿色时设置颜色。 But when the expiration date is red但是当到期日期是红色的

 .curentDate { color: rgb(54, 168, 54) }.expirationDate{ color: red; }
 <div class="form-group col"> <h5 for="maintenancePackage">Maintenance Package</h5> <p class="{{ setexpirationDate(ticket) }}">{{ getMaPackage(ticket) }}</p> </div>

app.ts应用程序.ts

  setexpirationDate(ticket) {
    let color = ''
    const endDate = moment(ticket.site.maEndDate.seconds * 1000).format('L');
    const currentDate = new Date()
    const currentDateFormat = moment(currentDate).format('L');
    if (endDate < currentDateFormat) {
      color = 'curentDate'
    } else {
      color = 'expirationDate'
    }
  }

You need to return the color variable您需要返回颜色变量

setexpirationDate(ticket) {
    let color = ''
    const endDate = moment(ticket.site.maEndDate.seconds * 1000).format('L');
    const currentDate = new Date()
    const currentDateFormat = moment(currentDate).format('L');
    if (endDate < currentDateFormat) {
      color = 'curentDate'
    } else {
      color = 'expirationDate'
    }
    return color // you need to return the value
  }

Your condition is wrong.你的条件不对。

if (endDate < currentDateFormat) {
      color = 'curentDate'
    } else {
      color = 'expirationDate'
    }

You are setting trying to set the color green when the date is expired and red for the opposite.您正在设置尝试在日期到期时将颜色设置为绿色,而在相反的情况下设置为红色

Solution:解决方案:

HTML: HTML:

<p [ngClass]="{isExpired?'expirationDate':'currentDate'}">{{ getMaPackage(ticket) }}</p>

TypeScript: TypeScript:

get isExpired() {
    let color = ''
    const endDate = moment(ticket.site.maEndDate.seconds * 1000).format('L');
    const currentDate = new Date();
    const currentDateFormat = moment(currentDate).format('L');
    return endDate < currentDateFormat;
  }

Have you tried adding?important on the color.你试过添加吗?对颜色很重要。 eg例如

.expirationDate{
    color:  red !important;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM