5

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?

5
  • 1
    Are you following everything in the Usage at github.com/urish/angular-moment mainly the sequence of <script> tags ? Commented Aug 18, 2014 at 18:38
  • Your angularMoment js file needs to be loaded before your application js file. Meaning angularMoment needs to be defined before it is injected into your factory. Make sure your files are loading in correct order. Commented Aug 18, 2014 at 18:40
  • There is no service named angularMoment, and also there is no .locale() method. May be it is a different library from the one that @bhantol mentioned? Commented Aug 18, 2014 at 18:41
  • I do not actually have a web page. I'm building some AngularJS services. I'm getting this error in my Jasmne tests. Commented Aug 18, 2014 at 20:14
  • Having the same issue, any conclusion on this? Commented Oct 9, 2015 at 14:07

4 Answers 4

2

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();
    }
  }
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to reference the angularMoment module when declaring your module. Otherwise it won't get loaded. That is what causes the $injector:unpr error. So you need something like this:

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

Comments

0

This is going to sound basic, but you are referencing the .js files in your html code, right?

1 Comment

I'm referencng the .js files in my Jasmine task. I'm running the code via Grunt.
0

This worked for me:

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

app.controller('myCtrl', function($scope, $log, moment) {
      var now = moment();
      $log.info(now.format("dddd, MMMM Do YYYY, h:mm:ss a"));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.