Search code examples
angularjsangular-ui-bootstrapangular-ui-typeahead

UI Bootstrap Typeahead not returning async values


I am trying to do an autocomplete search using UI Bootstrap Typeahead to select a category from a database.

I have set up an example database with 4 categories, the final one will contain more than a thousand categories.

When I start typing, the search results return 4 rows (so something must be working, because I tried to change the number of rows in the DB and the search result mirrors the total number of rows) but with no value. However, it doesn't matter what I write so it doesn't seem to be matching.

HTML

<input class="form-control" type="text" name="category" id="category" placeholder="Search..." ng-model="asyncSelected" typeahead="name for name in getCdOnCat($viewValue)" typeahead-loading="loadingLocations" required>

controller.js

//Typeahead: Category Search
$scope.getCdOnCat = function (searchVal) {
    return dataFactory.getCdOnCategory(searchVal, globalVal_accessToken, globalVal_storeId).then(function (response) {
        console.log(response.data.categories);
        return response.data.categories;
    }, function (error) {
        console.log('Error: dataFactory.getCdOnCategory');
    });
};

service.js

app.factory("dataFactory", function ($http) {
var factory = {};

factory.getCdOnCategory = function (searchVal, accessToken, storeId) {
    return $http.get('data/getCdOnCategory.aspx?searchVal=' + searchVal + '&accessToken=' + accessToken + '&storeId=' + storeId)
};

return factory;
});

JSON

{ "categories":[ 
    { "name": "Clothes" }, 
    { "name": "Cypress" }, 
    { "name": "Citrus" }, 
    { "name": "Cats" } 
] }

Solution

  • Please see here http://plnkr.co/edit/F4n1kNOHfZ9f3Zz63x2P?p=preview

    change

    <input class="form-control" type="text" name="category" id="category" 
     placeholder="Search..." ng-model="asyncSelected" 
     typeahead="name for name in getCdOnCat($viewValue)" typeahead-loading="loadingLocations" 
     required>
    

    to

    <input class="form-control" type="text" name="category" id="category" 
     placeholder="Search..." ng-model="asyncSelected" 
     typeahead="obj.name for obj in getCdOnCat($viewValue)" typeahead-loading="loadingLocations" 
     required>