簡體   English   中英

在API調用返回之前,Angular Typeahead返回-導致“未定義的長度”錯誤

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

我目前正在實現一個Angular提前輸入,每次用戶更改輸入時都會從API提取數據。 如果我添加typeahead-wait-ms=200則typeahead可以正常運行。 如果不這樣做,則會收到length of undefined.的錯誤length of undefined. 這是代碼:

的HTML

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

的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)
    }); 
}  

代碼通過后, $scope.userTypeAheadResults返回一個將在預輸入中顯示的userNames數組。 數組正確返回,但是在函數返回之前,錯誤已經在控制台中顯示出來,提示length of undefined 我在這里查看了關於stackoverflow的其他幾個問題,但沒有任何運氣。 有任何想法嗎? 提前致謝。

兌現承諾。

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM