How to use TimeAgoPipe of MomentModule from typescript file in angular 2

788 views Asked by At

I want to use moment.js for displaying the age of a marker shown in google map in my ionic 2 app. I have installed moment.js as

npm install --save angular2-moment

The doc of angular2-moment explains how to use amTimeAgo from template. But I couldn't find anything about using it from type script file. So I decided to try it myself.

First I imported TimeAgo pipe in my app.module.ts and provided it

import { TimeAgoPipe } from "angular2-moment";

...........

providers: [
   .............
   TimeAgoPipe
]

But when I create a constructor of this TimeAgoPipe as below by importing this file in a component I get the following error.

CODE :

import { TimeAgoPipe } from "angular2-moment";
...........
constructor(private timeAgoPipe: TimeAgoPipe) {}

ERROR :

Uncaught (in promise): Error: No provider for ChangeDetectorRef

1

There are 1 answers

14
FierceDev On

You need to add

import { ChangeDetectorRef } from '@angular/core';

In module and provide it.