Angular JS - Automatically focus input and show typeahead dropdown - ui.bootstrap.typeahead

后端 未结 11 1525
孤街浪徒
孤街浪徒 2020-11-30 03:07

I am using Angular JS - ui.bootstrap.typeahead:

I would like to click a button and focus an input field and automatically show the typeahead suggestion dropdown. I h

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 03:44

    I wanted the typeahead to open whenever my input element had focus. @yohairosen's solution didn't work for me on the latest version of Angular Bootstrap (Version: 1.0.3). Here's the solution that worked for me. It involved manually invoking the parser attached by ui-bootstrap-typeahead which populates the suggestions:

    angular.module('app')
    .directive('typeaheadFocus', function () {
      return {
          require: 'ngModel',
          link: function (scope, element, attr, ngModel) {
            element.bind('click', function () {
              ngModel.$parsers[0](ngModel.$viewValue);
            });
          }
        };
      };
    });

    This might be buggy because it assumes the parser added by ui-bootstrap-typeahead is the only one.

提交回复
热议问题