Javascript中的Angular2-moment Pipe [英] Angular2-moment Pipe in Javascript

查看:292
本文介绍了Javascript中的Angular2-moment Pipe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ionic3和 angular2-moment 。管道完美地用于格式化时间戳,例如:



html

 < ion-note class =small-text-search> 
上次上线{{personModel.lastAccessDate | amTimeAgo:true}}前
< / ion-note>

输出:

 上次上线19天前

这是我的 html 代码。不过,我想在 Javascript 字符串中执行此操作,前



ts

 '上次在线'+ personModel.lastAccessDate | amTimeAgo:true +'ago'; 

错误:

  Typescript Error无法找到名称'amTimeAgo'。 

问题



<有谁知道如何在javascript中使用管道?我该怎么编码呢?谢谢。

解决方案

大多数管道只是各个服务的薄包装。



某些管道具有难以替换的功能(例如内置角管)。在这种情况下,可以实例化管道类,并且可以调用 transform 方法。



很少有管道被设计用于仅使用编译器,例如使用 ChangeDetectorRef 与组件集成的编译器。 时间 - 以前管道是其中一个管道。



正确的方法是直接使用服务和库。顺便提一下,angular2-moment是Moment库的包装器。它应该是

 '上次在线'+时刻(personModel.lastAccessDate).fromNow()+'previous'; 


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.

解决方案

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

这篇关于Javascript中的Angular2-moment Pipe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆