【问题标题】:Angular 7/ Typescript Hide a Div based on time createdAngular 7 / Typescript根据创建的时间隐藏一个Div
【发布时间】:2020-09-16 00:01:21
【问题描述】:

我有 div 元素,我想在它的创建时间超过 6 个月前隐藏它。如何在 typescript 和 angular 7 中实现这一点

AngularJS - hide/show div if date is within last 6 months

我曾这样尝试过,它会引发错误“DateConstructor”类型上不存在属性“getMonth” HTML:

 <div class="request"  *ngIf="request.createTime>six_month_before_date">
<!-Only then display data-->
</div>

打字稿

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


     private getRequestDuration(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';
  }

【问题讨论】:

    标签: javascript html angular typescript


    【解决方案1】:

    问题与 TypeScript 和 Angular 本身无关,getMonth 方法不是静态的,因此无法通过其类名执行,需要使用实例:

    private getRequestDuration(request) {
        let six_month_before_date = new Date();
        six_month_before_date.setMonth(six_month_before_date.getMonth() - 6);// minus six month
        ...
    }
    

    更新:您的问题可能的解决方案是:

    HTML

    <div class="request" *ngIf="createTime > six_month_before_date">
    

    TypeScript

    @Component({ ... })
    export class YourClassComponent implements OnInit {
        ...
        createTime: Date;
        six_month_before_date: Date;
    
        ngOnInit() {
            const request = await doYourRequestHere();
            const duration = getRequestDuration(request); // Not being used now but may be needed somewhere else
            this.createTime = new Date(request.createTime);
            this.six_month_before_date = this.getSixMonthBeforeDate();
        }
    
        private getSixMonthBeforeDate() {
            const currentDate = new Date(); 
            currentDate.setMonth(currentDate.getMonth() - 6);
            return currentDate;
        }
    
        private getRequestDuration(request) {
            let createdDate = new Date(request.createTime);
            let timeInMilliSeconds = this.six_month_before_date.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';
        }
    }
    

    【讨论】:

    • 嗨大卫是 ng If 条件正确吗?
      six_month_before_date"> 它现在隐藏所有仪式,无论是否创建时间
    • 不完全是,request.createTime 是string,对吗?您需要使用Date 实例进行比较。
    • Six_month_before_date"> 这是正确的
    • 当然,只需分享您的组件代码的相关部分。假设 createdDate 和 six_month_before_date 是 Date 类型并且可用于模板,是的,它是有效的。
    • 嗨,大卫 public date = new Date() ; public Six_month_before_date= this.date.setMonth(this.date.getMonth() -6);
      six_month_before_date">
      这不起作用
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签