0
我想爲我的REST控制器使用一個工廠,該工廠返回一個字符串數組。我想在我的services.js中重用這個函數。
這是我的HTML(自動填充的輸入域)
<input type="text" ng-model="vertrag.vertrag.v00Lobcode.lobCode" typeahead-wait-ms="1000"
typeahead="lobcode for lobcode in getLobcode($viewValue)" typeahead-loading="loadingLobcodes">
控制器
$scope.getLobcode = function(val) {
Autocomplete.get(val).then(function(promise) {
console.log(promise);
});
};
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;
});
};
在此先感謝。