Angular2-moment Pipe in Javascript

2019-03-01 06:12发布

I am using Ionic3 with angular2-moment. The pipe works perfectly to format a timestamp, e.g.:

html

<ion-note class="small-text-search">
  Last Online {{personModel.lastAccessDate | amTimeAgo:true}} ago
</ion-note>

outputs:

Last Online 19 days ago

This is in my html code. I would however like to do this in a Javascript string, e.x

ts

'Last Online '+personModel.lastAccessDate | amTimeAgo:true +' ago';

error:

Typescript Error Cannot find name 'amTimeAgo'.

Question

Does anyone know how to use pipes in javascript? How would I code this? Thanks.

1条回答
够拽才男人
2楼-- · 2019-03-01 06:45

Most pipes are just thin wrappers around respective services.

Some pipes have functionality that is difficult to replace otherwise (e.g. builtin Angular pipes). In this case pipe class can be instantiated and transform method can be called.

Few pipes are designed to be used solely with compiler, like the ones that use ChangeDetectorRef to integrate with components. time-ago pipe is one of those pipes.

The proper way to do this is to use services and libraries directly. Incidentally, angular2-moment is a wrapper for Moment library. It should be

'Last Online '+ moment(personModel.lastAccessDate).fromNow() +' ago';
查看更多
登录 后发表回答