How can I programmatically open angular md-autocomplete?

I need to open the md-autocomplete angular materials drop-down list for a specific event. Is there any way to do this?

I have md autocomplete with a basic setup similar to this http://codepen.io/paucaverba/pen/GpogPv?editors=101

<div ng-controller="DemoCtrl as ctrl" layout="column" class="autocompletedemoBasicUsage" ng-app="MyApp">

  

  <md-autocomplete md-selected-item="ctrl.selectedItem" md-search-text="ctrl.searchText" md-items="item in ctrl.querySearch(ctrl.searchText)" md-item-text="item.display" md-min-length="0" placeholder="What is your favorite US state?">
    <md-item-template>
      <span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
    </md-item-template>
    <md-not-found>
      No matches found for "{{ctrl.searchText}}".
    </md-not-found>
  </md-autocomplete>
  <br>
        <button class="md-button md-raised"> open </button>
</form>

If I click on the control, it will open a drop-down list. And I want the same behavior when I click the open button

+4
source share
4 answers

Assign id="auto_complete_id"Attributemd-autocomplete

You can try something like this: (Tested. This should work. This will not work on codepen, but it will cause an error in the console)

codepen (Looking up elements via selectors is not supported by jqLite!)

.

HTML:

<button  ng-click=openAutocomplete()> Open </button>

JS:

$scope.openAutocomplete=function(){
   setTimeout(function(){
       angular.element('#auto_complete_id').find('input').focus();
   },0);
}
+1

autocompleteDirective autocomepleteController , md-focus md-blur,

+1

, - , .

( CodePen ) ,

angular.element('#testId').focus();

document.querySelector('#testId').focus();

document.getElementById('testId').focus();

jqLite, ( ) jQuery , , .

0

You can achieve what you are looking for by changing the button click code to

var elem = angular.element( document.querySelector( '#testId' ) );
elem.focus();

minimal and direct use.

Hello

0
source

Source: https://habr.com/ru/post/1607196/


All Articles