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

后端 未结 11 1524
孤街浪徒
孤街浪徒 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:52

    I got solved this issue through a directive. When you uses this directive, shows the list without some filter then, you type your search to find an element.

    angular.module('myapp')
    .directive('typeaheadLikeSelect', 
    ['$parse',function($parse) {
        return {
            require: 'ngModel',
            link: function (scope, element, attr, ngModel){
    
                var aux_modelValue, aux_viewValue,
                    modelGetter = $parse(attr.ngModel),
                    modelSetter = modelGetter.assign;
    
                var noViewValue = function(){
                  return
                  ngModel.$$lastCommittedViewValue === undefined ||
                  !ngModel.$$lastCommittedViewValue.trim();
                };
    
                var forceEvent = function(){
                  ngModel.$setViewValue();
                  ngModel.$viewValue = ' ';
                  ngModel.$setViewValue(' ');
                  ngModel.$render();
                  scope.$apply();
                  element.val(element.val().trim());
                };
    
                element.on('mousedown', function(e){
                  e.stopPropagation();
                  forceEvent();
                });
    
                element.on('blur', function(e){
                  e.stopPropagation();
                  if(aux_modelValue){
                    modelSetter(scope, aux_modelValue);
                    scope.$apply();
                  }
                });
    
                scope.$watch(function () {
                  return ngModel.$modelValue;
                }, function(newValue, oldValue){
                  if(newValue || (!newValue && !oldValue))
                    aux_modelValue = newValue;
                });
    
            }
        };
    }]);
    

    I leave a view code, to testing the above code.

    
        
    No Results Found

提交回复
热议问题