Javascript AngularJS工厂$http无法读取属性';长度';未定义的

Javascript AngularJS工厂$http无法读取属性';长度';未定义的,javascript,angularjs,Javascript,Angularjs,我想为我的REST控制器使用一个工厂,它返回一个字符串数组。我想重用我的services.js中的函数 这是我的HTML(自动完成输入字段) services.js AppServices.factory('Autocomplete', function($http, API_URL) { return { get: function(val) { var promise = $http.get(API_URL+'/leistobjekts/autocomplete/'+

我想为我的REST控制器使用一个工厂,它返回一个字符串数组。我想重用我的services.js中的函数

这是我的HTML(自动完成输入字段)

services.js

AppServices.factory('Autocomplete', function($http, API_URL) {
return {
    get: function(val) {
        var promise = $http.get(API_URL+'/leistobjekts/autocomplete/'+val.toUpperCase())
            .then(function(response) {
                return response.data;
        });
        return promise;
    }
};
}))

我的例外

TypeError: Cannot read property 'length' of undefined
at http://localhost:8080/bootstrap/js/ui-bootstrap-tpls-0.11.0.js:3553:24
at deferred.promise.then.wrappedCallback (http://localhost:8080/js/lib/angular/angular.js:11498:81)
at deferred.promise.then.wrappedCallback (http://localhost:8080/js/lib/angular/angular.js:11498:81)
at http://localhost:8080/js/lib/angular/angular.js:11584:26
at Scope.$get.Scope.$eval (http://localhost:8080/js/lib/angular/angular.js:12608:28)
at Scope.$get.Scope.$digest (http://localhost:8080/js/lib/angular/angular.js:12420:31)
at Scope.$get.Scope.$apply (http://localhost:8080/js/lib/angular/angular.js:12712:24)
at http://localhost:8080/js/lib/angular/angular.js:14220:36
at completeOutstandingRequest (http://localhost:8080/js/lib/angular/angular.js:4349:10)
at http://localhost:8080/js/lib/angular/angular.js:4650:7 
实际上,我得到了我需要的正确结果(控制台),但错误表明任何东西都不能正常工作。自动完成没有显示任何内容

这是我的自动完成功能,它已经为我工作了。但它并不是真正可重用的

$scope.getLobcode = function(val) {
    return $http({
        method: "GET",
        url: API_URL+'/leistobjekts/autocomplete/'+val.toUpperCase(),
        cache:true

    }).then(function(res){
        console.log(res.data);
        var lobcodes = [];
        angular.forEach(res.data, function(item){
            lobcodes.push(item);
        });
          return lobcodes;
        });
};

提前感谢。

像这样回报你的承诺:

$scope.getLobcode = function(val) {
    return Autocomplete.get(val);
};
或者干脆这样做:

$scope.getLobcode = Autocomplete.get;
$scope.getLobcode = function(val) {
    return Autocomplete.get(val);
};
$scope.getLobcode = Autocomplete.get;