首页 > 解决方案 > Javascript /Typescript/Angular 根据创建时间隐藏 div

问题描述

我在 Angular 7 中有一个动态表数据行,如果它是 6 个月前创建的,我想隐藏一条记录。我试过这样,现在仪式它隐藏所有记录而不考虑创建时间,任何人都可以更正 ngIf 条件并输入脚本代码,以便它只隐藏 6 个月前创建的记录

HTML

<div class="request"  *ngIf="request.duration > six_month_before_date">

打字稿

 public date = new Date() ;
 public six_month_before_date= this.date.setMonth(this.date.getMonth() -6); 

 private getRequestDuration(request) {
    console.log(request);
    let createdDate = new Date(request.createTime);
    let timeInMilliSeconds = this.currentDate.getTime() - createdDate.getTime();
    let seconds = timeInMilliSeconds / 1000;
    let minutes = seconds / 60;
    let hours = minutes / 60 + 5; // FIXME: EST Offset
    hours = hours > 0 ? hours : 0; // FIXME: Hack to address time-stamp conversions / daylight savings time
    let days = hours / 24;
    return (days > 0 ? Math.floor(days) + ' days, ' : '') + Math.floor(hours % 24) + ' hours';
  }

Request Object:
Object
agingCurrent: 121777671
agingTotal: 121902671
createTime: "2020-09-14T06:49:20"
createdBySso: "503184132"
duration: "1 days, 20 hours"
indErrored: false
indSavedToMdm: "TRUE"
indSubscribed: "TRUE"
partyId: "160598"
requestId: 627723
requestType: "Internal subscribe"
riskCategory: "ONE"
riskLevel: "MODERATE"
state: {status: "Active", label: "-"}
status: "APPROVED"
statusUpdateTime: "2020-09-14T06:51:25"
supplierId: "S18961"
supplierName: "LM Wind Power (Spain) SA"
transactionId: null
type: "SUBSCRIBE_INTERNAL"
updateTime: "2020-09-14T06:51:25"
version: 15
__proto__: Object

标签: javascriptangulartypescript

解决方案


听起来您只需要对以下内容进行一些调试:

*ngIf="request.createTime > six_month_before_date">

我解决这个问题的最简单方法是Debugging将其替换为以下内容:

*ngIf="foo(request.createTime)

并在您的 ts 创建一个函数:

    foo(requestCreatedTime) {
      console.log(this.date.setMonth(this.date.getMonth() -6));
      console.log(requestCreatedTime);
      return (this.date.setMonth(this.date.getMonth() -6); 
    }

这让您可以查看 ngIf 中使用的值,以确定它们是否符合您的预期。


推荐阅读