Search code examples
javascriptangularjscallbacktypeahead.js

AngularJS Typeahead callback function


I know there is something wrong related to this callback function. But I'm not sure what is the best way to fix this.

I have a resource:

.factory("AutoCompleteResource", function ($http) {
    return {
        getAutoComplete: function (searchTerm) {
            $http.get('/cdeCompletion/' + searchTerm, {}).then(function (response) {
                response.data;
            });
        }
    }
})

this is the controller:

 $scope.getAutoComplete = function (searchTerm) {
    AutoCompleteResource.getAutoComplete(searchTerm);
}

this is the html view:

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

What is the best way to fix the error here? Thanks!


Solution

  • You need to return the promise to the typeahead as it can handle promise and auto resolve it for data, i.e

    .factory("AutoCompleteResource", function ($http) {
        return {
            getAutoComplete: function (searchTerm) {
              //Return here
               return $http.get('/cdeCompletion/' + searchTerm, {}).then(function (response) {
                    return response.data; //return here
                });
            }
        }
    });
    

    and

    $scope.getAutoComplete = function (searchTerm) {
        //Return here
        return AutoCompleteResource.getAutoComplete(searchTerm);
    }