Given a list of items (think of it as a chat with multiple messages), I'd like to use moment.js to display the relative time (e.g. since creation) for each item.
Each item has its own component and relative time is displayed using the following code:
get timeFromNow(): string {
return moment(this.pageLoaded).fromNow(); // pageLoaded is a JS Date
}
The issue is that the component won't update the display of relative time, since the original input (pageLoaded
in the example above) won't change.
My question is: is there a best practice for dealing with such a scenario? Currently, I'm using markForCheck
of ChangeDetectorRef
to trigger re-rendering. However, I'm not sure if this is a good method performance-wise.
I've created a simple demo on Stackblitz. In the demo, I use the aforementioned markForCheck
to trigger the update.