【问题标题】:Time comparison in html using NGIF Condition - Angular使用 NGIF 条件的 html 时间比较 - Angular
【发布时间】:2018-09-27 10:48:46
【问题描述】:

我需要一个解决方案,我有一个 Android 应用程序的管理员发布应用程序。我已经为发布放置了一个删除按钮。要求是删除按钮必须在发布后显示 5 分钟。

这是我使用的 ngif 条件..

*ngIf="((((post.CreatedDate | date:'dd/MM/yyyy') == (PresentDate | date:'dd/MM/yyyy')) && ((post.CreatedDate | date:'HH') == (post.CreatedDate | date:'HH')))&&((post.CreatedDate | date:'mm')< (time)))" 

TS 页面中当前时间 + 5 分钟的代码

const d: Date = new Date();
this.PresentDate = d;
var x = new Date();
d.getHours(); // => 9
d.getMinutes(); // =>  30
this.time = d.getMinutes() +5;
this.hours = d.getHours();

请帮忙解决

【问题讨论】:

    标签: time angular-ng-if


    【解决方案1】:

    html 中的长表达式不是好习惯。

    *ngIf="canDeletePost(post)"

    canDeletePost(post) {
     return Date.now() - post.CreatedDate.getTime() <  5 * 60 * 1000;
    }
    

    如果CreatedDate 是 Js 日期。 5 * 60 * 1000 - 5 分钟,以毫秒为单位。实际上,在ngIf 中有一个方法也不是好的做法。

    无论如何,您不需要日期管道。管道用于改变视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2017-08-18
      • 2019-03-06
      • 1970-01-01
      • 2021-06-18
      相关资源
      最近更新 更多