我知道这个回调函数有问题。但我不确定解决这个问题的最佳方法是什么。
我有一个资源:
.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()">
此处修复错误的最佳方法是什么? 谢谢!
答案 0 :(得分:1)
您需要将承诺退还给预先承诺,因为它可以处理承诺并自动解决数据,即
.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);
}