add angular moments module.

272 views
Skip to first unread message

Janis Rough

unread,
Apr 24, 2016, 11:20:53 PM4/24/16
to AngularJS
my application script that runs the 4 directives

angular.module('angularApp', ['comment','commentList', 'commentForm', 'commentBox', XXXXXXX])

.run(function($rootScope){
$rootScope.comments = [{
}];

});

I want to add this 5th directive moment-module for angular moments.I tried adding it as a 5th directive and it can't find the moment-module.
angular
.module('moment-module', [])
.factory('moment', function ($window) {
return $window.moment;
});

I don't understand where it gets injected into?
tnx,

Sander Elias

unread,
Apr 24, 2016, 11:49:25 PM4/24/16
to AngularJS
Hi Janis,

You should add 'moment-module' to your angualrApp module's module array. From that moment on, it's available to the injector, and can you inject it where ever you need it.

Regards
Sander

Janis Rough

unread,
Apr 25, 2016, 2:19:53 AM4/25/16
to AngularJS
thanks,  I did just that but got an error. I added moment-module to the angularApp module array. It appears to load the two moment modules from the index.html.  
Don't know why bower can't put it at the top directory wants to add another bower directory but it did load but it says injection error? For the moment-module I just made a directive like the other 4 but I put it below.  
----error msg----
angular.js:80 Uncaught Error: [$injector:modulerr] Failed to instantiate module angularApp due to:
Error: [$injector:modulerr] Failed to instantiate module moment-module due to:
Error: [$injector:nomod] Module 'moment-module' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

------- moment-module  directive-----
'use strict';

/**
* @ngdoc directive
* @name angularApp.directive:moment-module
* @description
* # moment-module
*/
angular
.module('moment-module', [])
.factory('moment', function ($window) {
return $window.moment;
});

Sander Elias

unread,
Apr 25, 2016, 2:42:42 AM4/25/16
to AngularJS
Hi Janis,

Hmm, can you try the module-name without a dash? (Like in: "momentsModule") not too sure it will make a difference tough ;)


Regards
Sander

Sander Elias

unread,
Apr 25, 2016, 2:45:04 AM4/25/16
to AngularJS
Euhm, on second thought.
Are you sure you load both momentJS itself, and your moment.js module file? 

Regards
Sander

Message has been deleted
Message has been deleted

Janis Rough

unread,
Apr 25, 2016, 10:26:34 PM4/25/16
to AngularJS
 I have the momentModule directive loaded using the .run method in  app.js.  I  want to inject  the factory directive into a form on the rootScope, not scope.comment.  I don't have any controllers in this app.  Only the .run method and the 4 directives.  TIA
---factory directive-------
angular
.module('momentModule', [])

.factory('moment', function ($window) {
return $window.moment;
});

----my form-----
angular.module('commentForm', ['$window.moment' ??????? ])
.directive('commentForm', function () {

return {
template: '<form class="commentForm" name="form">' +
'<input type="text" placeholder="Your name" ng-model="comment.author" name="author"/>' +
'<input type="text" placeholder="Say something..." ng-model="comment.msg" name="msg"/>' +
'<input type="hidden" ng-model="comment.dateCreated" name="date" value="{{comment.dateCreated}}"/>' +
'{{comment.dateCreated }}'+

// '<input type="hidden" ng-model="comment.dateCreated" name="date" value="{{this.window = $window.moment}}"/>' +
'{{comment.dateCreated }}'+

// '<input type="hidden" ng-model="comment.dateCreated" name="date" />' +


'<input type="submit" value="Post" ng-click="submitComment()"/>' +
'</form>',

restrict: 'E',

link: function postLink(scope, element, attrs) {
scope.comment = {};

scope.submitComment = function(){
var comment = scope.comment;
comment.dateCreated = new Date();


if (!comment.msg || !comment.author) {
return;
}
scope.$emit('submitted', comment);
scope.comment = {};
}
}

};
});

Sander Elias

unread,
Apr 26, 2016, 12:28:17 AM4/26/16
to AngularJS
Your factory is called 'moment', so inject that, and not '$windows.moment' .


Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Janis Rough

unread,
May 2, 2016, 2:39:56 PM5/2/16
to AngularJS
Okay, Sander for what it is worth, I got the angular moment plug in working.  Thought I would let anyone know.  The problem was the injection factory only had moment.  So I guess I was missing angularMoment module.  It looked like to me with all that I read that the time-ago worked with just moment module.  In any case I figured it out after almost giving up it dawned on me that I could try just adding the angularmoment module as a directive inthe app.js.  It totally worked without any injection window or anything.  thanks for your input without it I wouldn't have gotten it.
Reply all
Reply to author
Forward
0 new messages