我知道与此回调函数相关的问题。但我不确定解决此问题的最佳方法是什么。
我有资源:
.factory("AutoCompleteResource", function ($http) {
return {
getAutoComplete: function (searchTerm) {
$http.get('/cdeCompletion/' + searchTerm, {}).then(function (response) {
response.data;
});
}
}
})
这是控制器:
$scope.getAutoComplete = function (searchTerm) {
AutoCompleteResource.getAutoComplete(searchTerm);
}
这是html视图:
<input name="searchTerms" id="searchTerms" type="text" class="form-control" ng-model="ftsearch"
ng-maxlength="500" placeholder="Search Common Data Elements"
typeahead-wait-ms="500" typeahead-min-length="3"
typeahead="searchQuery for searchQuery in getAutoComplete($viewValue) | filter:$viewValue | limitTo:8"
typeahead-focus-first="false" typeahead-on-select="gotoSearch()">
在这里修复错误的最佳方法是什么?
谢谢!
最佳答案
您需要将promise返回给typeahead,因为它可以处理promise并自动将其解析为数据,即
.factory("AutoCompleteResource", function ($http) {
return {
getAutoComplete: function (searchTerm) {
//Return here
return $http.get('/cdeCompletion/' + searchTerm, {}).then(function (response) {
return response.data; //return here
});
}
}
});
和
$scope.getAutoComplete = function (searchTerm) {
//Return here
return AutoCompleteResource.getAutoComplete(searchTerm);
}
关于javascript - AngularJS Typeahead回调函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31731528/