MVC和Angularjs:promise不等待数据

MVC和Angularjs:promise不等待数据,angularjs,asynchronous,promise,angular-promise,Angularjs,Asynchronous,Promise,Angular Promise,我是angularjs的新手,我在互联网上进行了研究,但我找不到任何适合我的问题的解决方案。我通过http调用从控制器获取一些数据。控制器侧正常。但客户端,承诺不等待数据。这里是我写的代码 //service code angular.module("myApp").service('$myService', function ($http, $q) { this.getDataArray = function () { var defe

我是angularjs的新手,我在互联网上进行了研究,但我找不到任何适合我的问题的解决方案。我通过http调用从控制器获取一些数据。控制器侧正常。但客户端,承诺不等待数据。这里是我写的代码

//service code 
angular.module("myApp").service('$myService', function ($http, $q) {
            this.getDataArray = function () {
                var deferred = $q.defer();
                $http.get('../Home/GetDataArray')
                    .success(function success(response) {
                        deferred.resolve(response);
                    })
                    .error(function () {
                        console.log("error getting data array");
                        deferred.reject();
                    });

                return deferred.promise;
            };
    }

// controller-code
angular.module("myApp").controller('dataController', function ($scope, $http, $myService) {

        $scope.getDataFromService = function () {
            $myService.getDataArray().then(function (response) {
                    $scope.dataArray = response.data;
                });
        };
    });
}

当我在第一次调用getDataFromService方法时,$scope.dataArray为空,但在第二次调用时,$scope.dataArray被数据填充。问题在哪里?谢谢你的帮助。

我自己不是一个角度专家。当我遇到同样的问题时,我就是这样做的。试试这个:

控制器:

angular.module("myApp").controller('dataController',[ '$scope', 'Service1', '$http', function ($scope, Service1, $http) {
    var deferred = Service1.getDataArray().$promise;
            return deferred.then(function successCallback(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                $scope.dataArray = response.data;
            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
            })
    }])
和服务:

   var service = angular.module("myApp").service('myService', ['ngResource']);
  myService.factory('Service1', ['$resource',
  function ($resource) {
      return $resource('../Home/GetDataArray', {}, {
          get: { method: 'GET', isArray: true },
      });
  }])

这个想法是,您的服务不应该等待返回,您的控制器才是。所以你应该在你的控制器中等待承诺,而不是你的服务。在我的例子中,我使用工厂是因为,我在项目中就是这样解决的,如果你不想使用工厂,你可以尝试直接实现它。

我自己不是角度专家。当我遇到同样的问题时,我就是这样做的。试试这个:

控制器:

angular.module("myApp").controller('dataController',[ '$scope', 'Service1', '$http', function ($scope, Service1, $http) {
    var deferred = Service1.getDataArray().$promise;
            return deferred.then(function successCallback(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                $scope.dataArray = response.data;
            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
            })
    }])
和服务:

   var service = angular.module("myApp").service('myService', ['ngResource']);
  myService.factory('Service1', ['$resource',
  function ($resource) {
      return $resource('../Home/GetDataArray', {}, {
          get: { method: 'GET', isArray: true },
      });
  }])

这个想法是,您的服务不应该等待返回,您的控制器才是。所以你应该在你的控制器中等待承诺,而不是你的服务。在我的例子中,我使用工厂是因为,我在项目中就是这样解决的,如果你不想使用工厂,你可以尝试直接实现它。

你能检查fiddler/chrome dev tools中第一次HTTP调用的结果吗?我也检查了它,但当我第一次调用时,$scope.dataArray为空,因为承诺不会等待,因为我说的不是$scope.data,而是HTTP调用本身。你绝对确定第一次调用的响应(如Chrome开发工具中所示)包含数据吗?不,第一次调用不包含数据。如果第一次调用不包含数据,问题在于服务器。你能检查fiddler/Chrome开发工具中第一次HTTP调用的结果吗?我也检查了它,但当我进行第一次调用时,$scope.dataArray为空,因为承诺不会等待,因为我说的不是$scope.data,而是HTTP调用本身。您是否绝对确定第一次调用的响应(如Chrome dev tools中所示)包含数据?不,第一次调用不包含数据。如果第一次调用不包含数据,则问题出在服务器上