0

我想从模型提供者那里检索数据,但我得到的只是控制器中的“未定义”。

这是代码:

控制器

pdmAtWeb.controller('SearchCtrl', function($scope, ItemModel){
$scope.updateTableFromSearch = function(){
        $scope.myData = ItemModel.findAllItems();
        console.log($scope.myData);
};});

提供者

pdmAtWeb.provider('ItemModel', function () {
this.defaultEndpoint = '/item';
this.defaultServiceUrl = 'http://localhost:8080/webservice';

this.setDefaultEndpoint = function (newEndpoint) {
    this.defaultEndpoint = newEndpoint;
};

this.setDefaultServiceUrl = function (newServiceUrl) {
    this.defaultServiceUrl = newServiceUrl;
}

this.$get = function ($http) {
    var endpoint = this.endpoint;
    var serviceUrl = this.serviceUrl;

    var refreshConnection = function () {
        // reconnect
    }
    return{
        findAllItems: function () {
            $http({method: 'GET', url: serviceUrl + endpoint}).
                success(function (data, status, headers, config) {
                    console.log(data);
                    return data;

                }).
                error(function (data, status, headers, config) {

                });
        }
    }
}});

提供者“ItemModel”从 Web 服务接收正确的数据。也许这是一个异步问题,但我不确定。

更新

添加延迟/承诺实现后,它按预期工作。这是最终代码:

控制器

pdmAtWeb.controller('SearchCtrl', function($scope, ItemModel){
$scope.updateTableFromSearch = function(){
       ItemModel.findAllItems().then(function(data){
          console.log(data);
           $scope.myData = data;
       });
};
});

提供者

pdmAtWeb.provider('ItemModel', function () {
    this.defaultEndpoint = '/item';
    this.defaultServiceUrl = 'http://localhost:8080/webservice';

    this.setDefaultEndpoint = function (newEndpoint) {
        this.defaultEndpoint = newEndpoint;
    };

    this.setDefaultServiceUrl = function (newServiceUrl) {
        this.defaultServiceUrl = newServiceUrl;
    }

    this.$get = function ($http, $q) {
        var endpoint = this.defaultEndpoint;
        var serviceUrl = this.defaultServiceUrl;

        var refreshConnection = function () {
            // reconnect
        }
        return{
            findAllItems: function () {
                var deferred = $q.defer();

                 $http({method: 'GET', url: serviceUrl + endpoint}).
                    success(function (data, status, headers, config) {
                     deferred.resolve(data);
                    }).
                    error(function (data, status, headers, config) {
                      deferred.reject();
                    });
                return deferred.promise;
            }
        }
    }
});
4

1 回答 1

0

您不需要延迟来完成此操作。你$http已经返回了一个承诺。在您的第一个示例中,您得到未定义的原因是因为您没有返回任何内容。检查您的findAllItems. 它没有返回任何东西。

如果你做return $http.get(.....)所有事情都应该在不显式使用 deferred 的情况下工作。

这是更正的版本:

pdmAtWeb.provider('ItemModel', function () {
this.defaultEndpoint = '/item';
this.defaultServiceUrl = 'http://localhost:8080/webservice';

this.setDefaultEndpoint = function (newEndpoint) {
    this.defaultEndpoint = newEndpoint;
};

this.setDefaultServiceUrl = function (newServiceUrl) {
    this.defaultServiceUrl = newServiceUrl;
}

this.$get = function ($http) {
    var endpoint = this.endpoint;
    var serviceUrl = this.serviceUrl;

    var refreshConnection = function () {
        // reconnect
    }
    return{
        findAllItems: function () {
            //NOTE addition of return below.
            return $http({method: 'GET', url: serviceUrl + endpoint}).
                success(function (data, status, headers, config) {
                    console.log(data);
                    //NOTE: YOU SHOULD ALSO return data here for it to work.  
                    return data;

                }).
                error(function (data, status, headers, config) {

                });
        }
    }
}});
于 2013-03-25T10:10:29.617 回答