Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Module in AngularJS Service

I'm learning AngularJS. Currently, I'm trying to load a third-party module in my service. Specifically, I'm trying to load angular-moment. My service is defined like this:

myApp.factory('myService', ['angularMoment', function(angularMoment) {
  return {
    getLocale: function() {
      return angularMoment.locale();
    }
  }
}]);

If I replace return angularMoment.locale() with return 'someLocale'; my code runs. However, as soon as I reference angularMoment, I get errors. I know it has something to do with the fact that I'm not loading the module correctly. However, I do not know what I'm doing wrong. I just see this error when I run my unit tests:

Error: [$injector:unpr] http://errors.angularjs.org/1.2.22/$injector/unpr?p0=angularMomentProvider%20%3C-%20angularMoment%20%3C-myService (line 36) (1)

What am I doing wrong?

like image 242
JQuery Mobile Avatar asked Nov 01 '22 18:11

JQuery Mobile


1 Answers

Try 'moment' instead of angularMoment in the service/factor injector. This will give you the object from MomentJS

myApp.factory('myService', ['moment', function(moment) {
  return {
    getLocale: function() {
      return moment.locale();
    }
  }
}]);
like image 128
bhantol Avatar answered Nov 12 '22 16:11

bhantol