I am trying to implement an AngularJS service that takes a date.prototype function and passes it to multiple controllers. I inject the services in the controllers but for some reason it isn't working. What am I doing wrong? Thanks.
services.js:
var appStuff = angular.module('services', [])
appStuff.service ('ChronService', function ()
{
Date.prototype.formatSubtractDate = function (x)
{
var myTime = new Date();
myTime.setFullYear(myTime.getFullYear) - x);
return myTime;
};
Date.prototype.formatAddDate = function (x)
{
var myTime = new Date();
myTime.setFullYear(myTime.getFullYear) + x);
return myTime;
};
});
oneController.js:
angular.module('oneController', [])
.controller('oneCtrl', function ($scope, ChronService) {
$scope.subtractYears = ChronService.Date().formatSubtractDate(4);
if ($scope.subtractYears >= 2004) {
console.log("right")
}
else
{
console.log("incorrect")
}
});
twoController.js
angular.module('oneController', [])
.controller('oneCtrl', function ($scope, ChronService) {
$scope.addYears = ChronService.Date().formatAddDate(25);
if ($scope.addYears < 1996) {
console.log("right")
}
else
{
console.log("incorrect")
}
});
asked Aug 31, 2016 at 14:08
fragilewindows
1,4101 gold badge16 silver badges26 bronze badges
1 Answer 1
you need to add the dependency for the module to the modules for the controllers. angular.module('oneController', ['services'])
currently your modules for your controllers don't know where the service is located.
answered Aug 31, 2016 at 15:15
Jeffrey Jarry
1562 silver badges6 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default