0

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")
    }
});

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

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.