2017-01-21 2 views
3

구성 요소에 타임 스탬프가있는 개체가 있습니다.템플릿의 현재 날짜를 확인하려면 어떻게해야합니까?

내가하려고하는 것은 템플릿의 런타임에 타임 스탬프를 확인하는 것입니다. 예를 들어 회원의 사용자 온라인 상태를 표시합니다. 온라인 인 경우 녹색 점을 표시하고 그렇지 않으면 런타임 중에 빨간색 점이 점등으로 전환되어야합니다.

<div class="show_member_line"> 
    {{member.membername}} 
    <div *ngIf="member.lastactivity > (!!!new Date().getTime()!!! - 120)" class="status_online"></div> 
    <div *ngIf="member.lastactivity < (!!!new Date().getTime()!!! - 120)" class="status_offline"></div> 
</div> 

누구나 아이디어가 있습니까? :)

답변

4
내 의견

더 나은 접근 방식은 같은 구성 요소에 타이머를 생성하는 것입니다

import {Component, OnInit, OnDestroy} from '@angular/core'; 

private timerSubscription: any = null; 
private lastActivityTime: Date; 

ngOnInit(){ 
    let timer = Observable.timer(1000, 1000); 
    this.timerSubscription = timer.subscribe((t:any) => { 
     this.timerExecuted(); 
    }); 
}  

private timerExecuted(): void { 
    var currentDate = new Date(); 
    currentDate.setHours(currentDate.getHours() - 2); //subtract 2 hours or 120 minutes from current date 
    this.lastActivityTime = currentDate; 
} 

ngOnDestroy() { 
    //unsubscribe the subscription in ngDestroy 
    if (this.timerSubscription != null) 
     this.timerSubscription.unsubscribe(); 
} 
lastActivityTime 각 초 후에 업데이트됩니다 그리고 당신은

처럼 템플릿에 더 쉽게 비교를 위해 사용할 수

<div class="show_member_line"> 
    {{member.membername}} 
    <div *ngIf="member.lastactivity > lastActivityTime" class="status_online"></div> 
    <div *ngIf="member.lastactivity < lastActivityTime" class="status_offline"></div> 
</div> 
관련 문제