Reputation: 1185
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
Reputation: 1162
Don't use any
, declare without it
import * as moment from 'moment';
moment = moment;
Upvotes: 5
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
Reputation: 9697
remove * as
form import
For install moment
from npm install moment --save
import moment from 'moment';
export class ChatComponent {
moment: any = moment;
}
<div>{{moment(activeTeam.createdAt).format('LL')}}</div>
Upvotes: 0
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
Reputation: 1071
Instead of declaring moment's type as any, declare it as moment: () => any;
Upvotes: 0