0

I'm new to AngularJS, and I am trying to access a returned value from a Factory Service Function myFactoryService, in order to use it in my Controller:RequestsController.

The code goes like this. First we have HolidaysService. This is responsible for obtaining an array of Dates from the server side (in C#) and returns it if the operation was successful:

app.factory('HolidaysService', function ($http) {
    var fac = {};
    fac.getHolidays = function (d) {
        return $http({
            method: 'GET',
            url: '/RequestAng/getHolidays'
        }).then(function successCallback(response) {
            var dates = []; // empty array of Holidays
            var i = 0;
            var counter = 0;
            for (var i in response.data) {
                dates[i] = new Date((parseInt(response.data[i].substr(6)))); // parsing the returned JSON Values.
                i++;
            }
            return dates;
        }, function errorCallback(response) {
            console.log(response);
        });
    }
    return fac;
});

Then I have another Factory which uses HolidaysService,defined above, in order to get the array of dates and do some computations:

 app.factory('myFactoryService', function (HolidaysService ) {
    var service = {};
    var counter = 0;
    service.Calc = function (d1, d2) {
        HolidaysService.getHolidays().then( 
            function (dates) {
                var i = 0;
                for (i in dates) {
                   if (dates[i] >= d1 && dates[i] <= d2) {
                       if (dates[i].getDay() != 6 && dates[i].getDay() != 7) {
                          counter++; // This is the value I need to access in the controller later on
                       }
                   }
                   i++;
                }
                return counter;
            }
        ); 
    }
    return service;
});

Now I want to use the returned value from myFactoryService in my RequestsController as follows:

(function () {
var app = angular.module('RM', ['ui.date']);

app.controller('RequestsController', function ($scope, $http, myFactoryService) {

    $scope.counter=0;
    $scope.Request = {
       Remaining: '',
       DateRequest: new Date(),
       DateBeg: '',
       DateEnd: ''
    };

    /* This is to change the Changing 'Remaining' Input TextField if Dates are changed */

    var names = ['Request.DateBeg', 'Request.DateEnd'];
    $scope.$watchGroup(names, function (newValues, oldValues, $scope) {
       var businessDays = $scope.CalcBusinessDays(); // Calculates business days between two dates

       if (businessDays != -1) {
          var x = 1; 
          x = myFactoryService.Calc($scope.Request.DateBeg,$scope.Request.DateEnd);
          console.log("printing x: " + x);
       }
     });
 });

Now when I try to access the value returned from myFactroryService, I always get undefined. Any ideas? Thanks in advance. (Sorry for the long post)

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
Qarchli Ismail
  • 147
  • 2
  • 12

1 Answers1

1

I think you miss a return in front of HolidayService:

service.Calc = function (d1, d2) {
    return HolidaysService.getHolidays().then(
    // rest of code ignored

And here:

      x = myFactoryService.Calc($scope.Request.DateBeg,$scope.Request.DateEnd);

x is actually a promise. You can do:

      x.then(function(xval) { console.log("printing x: " + xval); }
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32