2016-05-13 55 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