UI-bootstrap typeahead does not resolve promise

。_饼干妹妹 提交于 2020-04-30 07:22:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!