【问题标题】:Ui.Bootstrap Typeahead not working with Async callUi.Bootstrap Typeahead 不适用于异步调用
【发布时间】:2015-06-17 01:43:04
【问题描述】:

我正在使用带有异步调用的 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 函数中的承诺有关。

任何想法为什么下拉菜单没有出现?

【问题讨论】:

    标签: angularjs node.js sails.js angular-bootstrap angular-ui-typeahead


    【解决方案1】:

    我将 Sails 从 10.5 更新到 11。随后客户端 SDK 发生了变化(Sails.io),并且我使用 Angular-Sails 的 Angular 库也需要更新。

    promise 函数发生了变化,我需要更新它。将 promise 函数更改为以下函数可以解决问题。现在 typeahead 函数得到响应并出现下拉菜单。

        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!');
            });
    

    【讨论】:

    • 在我的情况下,promise 函数正在工作,但返回数据不知何故没有进入下拉菜单。但我检查了静态数据,它工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    相关资源
    最近更新 更多