我正在嘗試使用我設置的REST資源來獲取Angular UI引導程序typeahead。但是我不確定如何讓它與異步性質一起工作。
目前我已經修改了Angular UI Bootstrap給出的例子。
所以我的html看起來像這樣,調用getLibs()來獲取typeahead下拉列表。
<div class='container-fluid' ng-controller="TypeaheadCtrl">
<pre>Model: {{selected| json}}</pre>
<input type="text" typeahead="lib.name for lib in getLibs($viewValue)" active='active' ng-model="selected" typeahead-min-length='3' >
</div>
我的資源看起來像這樣:
angular.module('LibraryLookupService', ['ngResource']).factory('Library',
function($resource){
return $resource( "../../api/seq/library/?name__icontains=:searchStr" , {} ,
{ 'get': {method:'GET'},
'query': {method:'GET', params: {} , isArray:false },
}
)
}
);
我的控制器看起來像這樣(我猜測這是我在這裏做一些不正確的):
function TypeaheadCtrl($scope , Library){
$scope.selected = undefined;
$scope.active = undefined ;
$scope.libs = [{name:'initial'} , {name:"values"}];
$scope.getLibs = function(viewValue){
console.log("value:", viewValue);
if (viewValue.length > 3) {
var return_vals = Library.query({searchStr:viewValue}, function() {
$scope.libs = return_vals.objects ;
console.log("here", $scope.libs) ;
}) ;
}
return $scope.libs
}
}
所以我的理解是,在預輸入菜單正在從getLibs的返回值填充( )功能。 當調用getLibs()時,它正在查詢REST接口,但最初會返回一個空值。這是由提供給Library.query()方法的函數填充的,並且這是在從REST請求返回數據之後完成的。
這意味着基本上菜單正在更新一個按鍵晚於我想要的。我鍵入'3456',並將其結果填充到REST接口的'345'查詢結果。
如何從Library.query()函數返回響應時更新菜單?我正確地處理這個問題嗎?
實際上,如果沒有使用then()方法,您可以返回$ promise,因爲它是多餘的。 – mameluc 2015-05-12 09:13:02
我不明白你爲什麼在調用PersonService時傳遞person對象,因爲我在工廠定義中看不到Person對象。 – peztherez 2015-12-05 16:08:20