javascript - AngularUI-Bootstrap 的 Typeahead 无法读取 `length` `undefined` 的属性

标签 javascript angularjs angular-ui angular-ui-bootstrap

当我尝试使用 Promise 从 AngularUI-Bootstrap 获取 typeahead 值时,出现以下错误。

TypeError: Cannot read property 'length' of undefined
  at http://localhost:8000/static/js/ui-bootstrap-tpls-0.6.0.min.js:1:37982
  at i (http://localhost:8000/static/js/angular.min.js:79:437)
  at i (http://localhost:8000/static/js/angular.min.js:79:437)
  at http://localhost:8000/static/js/angular.min.js:80:485
  at Object.e.$eval (http://localhost:8000/static/js/angular.min.js:92:272)
  at Object.e.$digest (http://localhost:8000/static/js/angular.min.js:90:142)
  at Object.e.$apply (http://localhost:8000/static/js/angular.min.js:92:431)
  at HTMLInputElement.Va.i (http://localhost:8000/static/js/angular.min.js:120:156)
  at HTMLInputElement.x.event.dispatch (http://localhost:8000/static/js/jquery-1.10.2.min.js:5:14129)
  at HTMLInputElement.v.handle (http://localhost:8000/static/js/jquery-1.10.2.min.js:5:10866) 

我的 HTML 标签是:
<input type="text" class="form-control" id="guestName" ng-model="name" typeahead="name for name in getTypeaheadValues($viewValue)">

用我的getTypeaheadValues执行以下操作的函数:
$scope.getTypeaheadValues = function($viewValue)
{
    // return ['1','2','3','4'];

    $http({
        method: 'GET',
        url: 'api/v1/person?name__icontains=' + $viewValue
    }).error(function ($data) {
        console.log("failed to fetch typeahead data");
    }).success(function ($data) {
        var output = [];
        $data.objects.forEach(function (person)
        {
            output.push(person.name);
        });
        console.log(output);
        return output;
    });
}

我不明白 AngularUI-Bootstrap 提示什么未定义。如果我删除最上面的评论 return这些值显示得很好。 console.log success 中的输出还返回我期望在数组中的所有值。

我错过了什么会导致 AngularUI-Bootstrap 看不到返回的数组?

最佳答案

这个问题是双重的。

首先是我没有返回 promise 事件($http 调用)。缺少 return 语句(正如@tobo 指出的那样)是直接导致错误的原因。我需要返回 promise ,而不是数组。

第二个是我需要使用 .then而不是 .success让 AngularUI-Bootstrap 获取结果。

我遇到了以下问题:
How to tie angular-ui's typeahead with a server via $http for server side optimization?

这将我的函数调用更新为以下内容:

$scope.getTypeaheadValues = function($viewValue)
{
    return $http({
        method: 'GET',
        url: 'api/v1/person?name__icontains=' + $viewValue
    }).then(function ($response) {
        var output = [];

        console.log($data);

        $response.data.objects.forEach(function (person)
        {
            output.push(person.name);
        });

        console.log(output);
        return output;
    });
}

关于javascript - AngularUI-Bootstrap 的 Typeahead 无法读取 `length` `undefined` 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19525184/

相关文章:

javascript - Angular JS 默认选中单选按钮

jquery-ui - 让 Angular UI 正常工作

javascript - Angular ui 路由器 : how do I reload a state when a path parameter changes but not reload when a query parameter changes?

javascript - ui-calendar fullcalendar 中每个事件的自定义按钮

javascript - 更新名称未知的对象的模型值

javascript - 在 Meteor.js 中调用同步函数

空格上的仅 Javascript wordwrap 函数?

javascript - 需要使用 angular-smart-table 进行正则表达式过滤

Angularjs $http.post 与内容类型 application/x-www-form-urlencoded 联系表格 7

javascript - 需要 OpenCPU 和 igraph 输出格式的帮助