我正在使用 ui.bootstrap typeahead 进行异步调用。然而,生成的数据似乎没有找到返回 typeahead 指令的方法,并且随后没有数据出现下拉列表。
html 是
<input type="text" ng-model="asyncSelected" placeholder="Search Profile" typeahead="username.username for username in getSearch($viewValue)" typeahead-wait-ms="400" class="form-control">
JS函数就在这里
$scope.getSearch = function (val) {
return $sails.get("/search/" + val).success(function (response) {
console.log(response);
return response.map(function (item) {
console.log(item.username);
return item.username;
});
}).error(function (response) {
console.log('The sails profile search has failed');
});
}
响应 JSON 对象是
Object
@type: "d"
id: "#17:3"
username: "Burt"
__proto__: Object
我正在客户端上使用 Angular-sails 来查询后端。我已经使用 ui.bootstrap 文档中给出的示例测试了代码,一切正常。
$sails.get 也可以作为 console.log(item.username) 打印出值。
我有一种感觉,它与 getSearch 函数中的 promise 有关。
有什么想法为什么下拉菜单没有出现吗?
最佳答案
我将 sails 从 10.5 更新到 11。随后客户端 SDK 发生了更改 (Sails.io),我使用 Angular-Sails 的 Angular 库也需要更新。
promise 功能发生了变化,我需要更新它。将 promise 函数更改为下面的函数解决了问题。现在,预先输入功能得到响应并出现下拉菜单。
return $sails.get("/search/" + val)
.then(function (response) {
console.log(response);
return response.data.map(function (item) {
return item.username;
});
}, function (response) {
alert('Houston, we got a problem!');
});
关于angularjs - Ui.Bootstrap Typeahead 不适用于异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29576662/