Search code examples
javascriptangularjsbootstrap-typeaheadangular-ui-typeahead

Typeahead Angular JS: Show results on focus after input blur


Codepen demo

As you notice, if you get typeahead to display any results, after you blur out the input the ng-repeat of the queries gets cleared out. Whilst I want it to hide but stay there so as it could be instantly shown again on refocus of the input.

How to get typeahead not to clear the ng-repeat list?

Exact demo of functionality I am trying to achieve

  1. Enter any letter
  2. Blur out the input
  3. Focus it again, relevant search results will instantly appear.

It's different from codepen I linked.


Solution

  • Editing the dismissClickHandler in the following way works around the problem.

    var dismissClickHandler = function (evt) {
        if (element[0] !== evt.target) {
            resetMatches();
            scope.$digest();
        } else if (element.val().length) {
            element.focus();
            hasFocus = true;
            getMatchesAsync(element.val());
        }
    };