Reputation: 12855
I have added with bower the angular-moment library.
I followed the how-to of the angular-moment author and added also both scripts to my index.html file and to the karma.conf.js file with the files option including certain files.
How can I access in my app/dateFactory the angular-moment object to do time stuff?
app/dateFactory
'use strict';
angular.module('clientApp', ['angularMoment']).factory('dateFactory', function () {
function dateRange(startDate, endDate) {
}
// Public API here
return {
dateRange: dateRange
}
});
UPDATE => running this code the factory instance is defined!
angular.module('clientApp').factory('dateFactory', function() {
function dateRange(startDate, endDate) {
var bla = moment.format()
return [];
//return Enumerable.range(0, endDate.Subtract(startDate).Days + 1).Select(offset => startDate.AddDays(offset)).toArray();
}
// Public API here
return {
dateRange: dateRange
}
});
UPDATE => running this code the factory instance is undefined!
angular.module('clientApp', ['angularMoment']).factory('dateFactory', ['moment', function(moment) {
function dateRange(startDate, endDate) {
moment.format() //for example
return [];
//return Enumerable.range(0, endDate.Subtract(startDate).Days + 1).Select(offset => startDate.AddDays(offset)).toArray();
}
// Public API here
return {
dateRange: dateRange
}
}]);
Thats the way the factory is created:
describe('Service: dateFactory', function () {
// load the service's module
beforeEach(module('clientApp'));
// instantiate service
var dateFactory;
beforeEach(inject(function (_dateFactory_) {
dateFactory = _dateFactory_;
}));
it('should do something', function () {
var data = dateFactory.dateRange(new Date(2014,1,1), new Date(2014,1,15));
expect(!!dateFactory).toBe(true);
});
});
Upvotes: 3
Views: 2357
Reputation: 639
You can inject the 'moment' global into your factory.
'use strict';
angular.module('clientApp', ['angularMoment']).factory('dateFactory', ['moment', function(moment) {
function dateRange(startDate, endDate) {
moment.format() //for example
}
// Public API here
return {
dateRange: dateRange
}
}]);
Upvotes: 3