Angular2-moment Pipe in Javascript

谁都会走 提交于 2020-01-21 21:21:23

问题


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:


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';


来源:https://stackoverflow.com/questions/44155526/angular2-moment-pipe-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!