简体   繁体   English

在API调用返回之前,Angular Typeahead返回-导致“未定义的长度”错误

[英]Angular Typeahead returns before API call has returned - causing 'Length of Undefined' error

I am currently implementing an Angular typeahead that is pulling data from an API each time the user changes the input. 我目前正在实现一个Angular提前输入,每次用户更改输入时都会从API提取数据。 The typeahead functions correctly if I add typeahead-wait-ms=200 . 如果我添加typeahead-wait-ms=200则typeahead可以正常运行。 If I do not, I get an error of length of undefined. 如果不这样做,则会收到length of undefined.的错误length of undefined. Here is the code: 这是代码:

HTML 的HTML

<input type="text" ng-model="selectedUser" typeahead="user for user in userTypeAhead($viewValue)">

JavaScript 的JavaScript

$scope.userTypeAhead = function(userTypeAheadInput){
    myService.searchUserTypeAhead(userTypeAheadInput).then(function(data) {

        $scope.userTypeAheadResults = [];

        for (i = 0; i < data.array.length; i++) {
             $scope.userTypeAheadResults.push(data.array[i].userName);
        }

        return $scope.userTypeAheadResults;

    }, function(error) {
        console.log(error)
    }); 
}  

When the code goes through, $scope.userTypeAheadResults returns an array of userNames that will show in the typeahead. 代码通过后, $scope.userTypeAheadResults返回一个将在预输入中显示的userNames数组。 The array returns correctly, but before the function has returned, the error has already showed up in the console saying length of undefined . 数组正确返回,但是在函数返回之前,错误已经在控制台中显示出来,提示length of undefined I have looked at several other questions here on stackoverflow and haven't had any luck. 我在这里查看了关于stackoverflow的其他几个问题,但没有任何运气。 Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

Return the promise. 兑现承诺。

$scope.userTypeAhead = function(userTypeAheadInput){
    // return the promise
    return myService.searchUserTypeAhead(userTypeAheadInput).then(function(data) {

        $scope.userTypeAheadResults = [];

        for (i = 0; i < $scope.data.array.length; i++) {
             $scope.userTypeAheadResults.push(data.array[i].userName);
        }

        return $scope.userTypeAheadResults;

    }, function(error) {
        console.log(error)
    }); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2-API请求未返回Observable,错误:未定义没有属性“ length” - Angular 2 - API Request not returning Observable, Error: undefined has no property 'length' 提前输入有时返回未定义 - Typeahead Sometimes Returns Undefined Angular js验证函数在验证的api调用完成之前返回 - Angular js validation function returns before the api call for validation is completed Angular从API调用返回的数据为[Object Object],但在组件中未定义 - Angular returns data from an API call as [Object Object], but undefined in the component Angular - 管道从其中包含条件的方法调用返回可观察的 - 无法读取未定义的属性“订阅” - Angular - Piping returned observable from method call which has a conditional in it - Cannot read property 'subscribe' of undefined JavaScript typeof Typeahead返回未定义 - Javascript typeof Typeahead returns undefined angular-ui引导程序提前输入换码键导致输入字段值未定义 - angular-ui bootstrap typeahead escape key causing input field value become undefined 在 API 返回 Angular 中的数据之前添加 sleep - Adding sleep before API returned the data in Angular 异步错误捕获器包装器 function 导致我返回的 promise 未定义 - asynchronous error catcher wrapper function causing my returned promise to be undefined 尝试处理从 NodeJS 中的 API 调用返回的 json 文件 - 错误:未定义 - 为什么? - Trying to process json file returned from an API call in NodeJS - error: undefined - why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM