UI Bootstrap Typeahead不返回异步值

时间:2014-10-31 09:12:24

标签: angularjs angular-ui-bootstrap angular-ui-typeahead

我正在尝试使用UI Bootstrap Typeahead进行自动完成搜索,以便从数据库中选择一个类别。

我已经建立了一个包含4个类别的示例数据库,最后一个将包含超过一千个类别。

当我开始输入时,搜索结果会返回4行(因此必须有效,因为我尝试更改数据库中的行数,搜索结果会反映总行数),但没有值。但是,我写的并不重要,所以它似乎不匹配。

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" } 
] }

1 个答案:

答案 0 :(得分:3)

请参阅此处http://plnkr.co/edit/F4n1kNOHfZ9f3Zz63x2P?p=preview

更改

<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>

<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>