samar taj Shaikh
samar taj Shaikh

Reputation: 1185

moment is giving me error in angular 5 html template

I am using moment v2.22.0 in angular 5, and this is how I have imported it in module -

import * as moment from 'moment';

and using it in component as -

export class ChatComponent {
  .
  .
  .
  public moment: any = moment;
  .
  .
}

and when I am using it in html template -

<div>{{moment(activeTeam.createdAt).format('LL')}}</div>

it is giving me an error message that -

[Angular] Member 'moment' is not callable

can anyone tell me what I am doing wrong !!

Upvotes: 5

Views: 9091

Answers (5)

Suroor Ahmmad
Suroor Ahmmad

Reputation: 1162

Don't use any, declare without it

import * as moment from 'moment';
moment = moment;

Upvotes: 5

Bhaskararao Gummidi
Bhaskararao Gummidi

Reputation: 2593

cli (run in console)

// install moment js
npm install moment --save

component declaration (ts)

// import and declare moment
import * as moment from 'moment';
moment: any = moment;

template file (html)

// template syntax
<p>{{moment(date_time_variable).format('ll')}}</p>

Upvotes: 22

Krishna Rathore
Krishna Rathore

Reputation: 9697

remove * as form import

For install moment from npm install moment --save

Stackblitz Demo

import  moment from 'moment';

export class ChatComponent {
  moment: any = moment;
}

<div>{{moment(activeTeam.createdAt).format('LL')}}</div>

Upvotes: 0

Akj
Akj

Reputation: 7231

Try to import moment as :

import  moment from 'moment';

HTML:

<div>{{getFormat(activeTeam)}}</div>

TS:

getFormat(activeTeam){
   return moment(activeTeam.createdAt).format('LL')
}

Upvotes: 4

abdullahkady
abdullahkady

Reputation: 1071

Instead of declaring moment's type as any, declare it as moment: () => any;

Upvotes: 0

Related Questions