最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular19 - Angular: linkedSignal and relativeTimestamp - Stack Overflow

programmeradmin0浏览0评论

I want to display a relative timestamp. In the Angular template I have

{{relativeTime}}

and in the component I get the input as

timeStamp = input.required<Date>();

The calculation of the relative time is done like

relativeTime='Just now';
updateRelativeTime(): void {
        if (Date.now() - this.timeStamp().getTime() > 30000) {
            this.relativeTime= DateTime.fromJSDate(this.timeStamp()).toRelative() ?? '';
        }
    }

So far the code works as expected if I stich it together. Additionally I have in the constructor an update every 5 seconds (in the first minute)

    timer(30000, 5000)
        .pipe(take((60 - 30) / 5))
        .subscribe(() => {
            this.updateRelativeTime();
        }),

My problem is if the timeStamp changes I want to have a new calculation. I was thinking about the new Angular 19 feature with linkedSignal but somehow I don't see how they come togehter :-/ I can do an inital computation and set something like

relativeTime = linkedSignal(() => updateRelativeTime());

But than it would be the same as computed. The problem with computed is that I need to updated it via interval... So, how to do it properly?

发布评论

评论列表(0)

  1. 暂无评论