AngularJS提前类型回调函数

laawzig2  于 2022-10-31  发布在  Angular
关注(0)|答案(2)|浏览(173)

我知道这个回调函数有问题,但是我不知道什么是最好的解决方法。
我有一个资源:

.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()">

什么是最好的方法来修复这里的错误?谢谢!

des4xlb0

des4xlb01#

您需要将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);
}
2skhul33

2skhul332#

您需要调用bs-options

<input bs-typeahead id="txtJobNumber" type="text" class="form-control typeahead"
                   typeahead-wait-ms="10" typeahead-append-to-body="true"
                   ng-model="JobNumber" bs-on-select="OnJobSelected"
                   bs-options="item.JobNumber for item in getMatchedJobNumbers($viewValue)" 
                   data-limit="Infinity" data-min-length="3" data-html="1" data-auto-select="true" />

当用户在typeahead输入框中键入内容时,添加此命令以调用Api

$scope.getMatchedJobNumbers= function (value) {

    }

相关问题