问题
Problem description
Hi, I have a problem with UI-Bootstrap's typeahead, there is standard example on the homepage that demonstrates typeahead usage:
http://plnkr.co/edit/LS5OMsnQtdsJ87eW4pjG?p=preview
This example works pretty fine until I start using promises with typeahead
directive:
http://plnkr.co/edit/ZuUBDOPcOJIW0Bkskrb5?p=preview
The change is pretty simple, I've replaced direct variable initialisation with delayed initialisation using $timeout service, as a result typeahead stops working
Question:
What am I doing wrong? It is clearly stated that UI-Bootstrap's typeahead: works with promises and it means that you can retrieve matches using the $http service with minimal effort
Thank you,
回答1:
You should be returning a promise that resolves to matched results:
$scope.getStates = function($viewValue) {
return $timeout(function () {
return filterFilter(['Alabama', 'Alaska', ...], $viewValue);
}, 1000);
};
and then in HTML:
<input type="text" ng-model="selected" typeahead="state for state in getStates($viewValue)">
Here is a working plunk: http://plnkr.co/edit/RAkzX0UoWHVLUOZ6jEyA?p=preview
The way you've written your expression in a promise being filtered.
来源:https://stackoverflow.com/questions/17897002/ui-bootstrap-typeahead-does-not-resolve-promise