2016-05-13 54 views
0

我的HTML代碼是:AngularJS UI引導預輸入從未顯示結果

<p> 
    <input type="text" ng-model="destination" placeholder="Destination" 
     uib-typeahead="dest as dest.name for dest in getDestinations($viewValue)" typeahead-loading="loadingDestinations" 
     typeahead-no-results="noResults" typeahead-min-length="3"> 

    <i ng-show="loadingDestinations">...</i> 

    <div ng-show="noResults"> 
     <i>xxx - </i> No Destinations Found 
    </div> 
</p> 

而且我getDestinations()函數,該函數返回一個HttpPromise:

$scope.getDestinations = function(viewValue) { 
    console.log(viewValue); 
    return $http.get('/api/destAutoComplete', { 
     params : { 
      prefix: viewValue, 
      countryId: 94 
     } 
    }); 
} 

它返回輸入的響應「IST 「從服務器:

[ 
{ 
"name": "Costa del Sol/Istan", 
"cityId": 5452, 
"locationId": 30083 
}, 
{ 
"name": "Istanbul", 
"cityId": 1122, 
"locationId": null 
} 
] 

當你看到服務器正確過濾結果作爲json數組,但鍵入從不顯示結果,並始終顯示「找不到目的地」。我究竟做錯了什麼?我試圖在「typeahead」下拉列表中將「name」顯示爲標籤,並在選擇其中一個時將整個dest對象設置爲範圍內的目標。

回答

1

這是因爲你忘了返回你的承諾對象。根據Angular Bootsrap UI code comment

任何返回承諾對象的函數都可用於異步加載值。

試試這個

$scope.getDestinations = function(viewValue) { 
    console.log(viewValue); 
    return $http.get('/api/destAutoComplete', { 
     params: { 
      prefix: viewValue, 
      countryId: 94 
     } 
    }).then(function(response) { 
     // or whatever response you are getting 
     return response.data.results; 
    });; 
} 

Plunker:http://plnkr.co/edit/rIsiEjDBajZb0JPaNjZB?p=preview