urish / angular-moment

Moment.JS directives for Angular.JS (timeago and more)
MIT License
2.6k stars 397 forks source link

moment is not a function inside controller / How to inject moment-timezone? #291

Closed felixfrtz closed 7 years ago

felixfrtz commented 7 years ago

Hey,

I configured my app according to the documentation provided like such:

app.js

var app = angular.module('myApp', ['angularMoment']);

index.html

    <script src="/js/app.js"></script>
    <script src="/js/controller.js"></script>

    <script src="/../node_modules/moment/moment.js"></script>
    <script src="/../node_modules/moment-timezone/moment-timezone.js"></script>
    <script src="/../node_modules/angular-moment/angular-moment.js"></script>

controller.js

var app = angular.module('myApp', ['angularMoment']);
app.controller('myController',['$scope', '$http', '$rootScope', 'moment', function($scope, $http, $rootScope, moment){

moment(2017-02-14T17:31:00.000Z).tz('America/New_York'); //undefined

Here, I am getting the undefined error. What I want to achieve is converting times given in UTC to the specific timezone of New York. Why is moment not a function there? In the documentation it says

If you wish to use moment() in your services, controllers, or directives, simply inject the moment variable into the constructor.

Which is what I did, or not?

Furthermore, I am struggling to understand if I have to do the configuration for moment-timezone like it is shown in the documentation:

ngModule.constant('moment', require('moment-timezone'));

Where does this go? In the app.js or in the controller? I am getting require is not defined in both cases. Do I actually need this for doing above conversion?