This question already has answers here:
How do I return the response from an asynchronous call?
                            
                                (39个答案)
                            
                    
                3年前关闭。
        

    

我是AngularJS的新手,我试图从Factory Service Function myFactoryService访问返回的值,以便在我的Controller:RequestsController中使用它。

代码是这样的。首先,我们有HolidaysService。这负责从服务器端(在C#中)获取日期数组,如果操作成功,则将其返回:

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


然后,我有另一个工厂使用上面定义的HolidaysService来获取日期数组并进行一些计算:

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


现在,我要在我的myFactoryService中使用从RequestsController返回的值,如下所示:

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


现在,当我尝试访问从myFactroryService返回的值时,我总是会得到undefined。有任何想法吗?提前致谢。 (对不起,很长的帖子)

最佳答案

我认为您错过了return前面的HolidayService

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


和这里:

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


x实际上是一个承诺。你可以做:

      x.then(function(xval) { console.log("printing x: " + xval); }

关于javascript - 如何从angularJS的服务工厂函数访问返回的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41168173/

10-17 02:21