Angular Material: md-autocomplete - how to hide md-autocomplete-suggestions on Enter event?

后端 未结 5 2061
旧时难觅i
旧时难觅i 2021-02-13 04:48

I have md-autocomplete:



        
5条回答
  •  我在风中等你
    2021-02-13 05:51

    If you don't mind losing focus on the md-autocomplete input element on enter, you can close md-autocomplete suggestions without using hacky ways that involve messing with the internal $mdAutocompleteCtrl controller. This depends on md-autocomplete to automatically hide suggestions when the input element is no longer focused.

    1. Wrap your md-autocomplete element in a form (or use a directive like ng-enter) and add an ID to the input element using md-input-id
    
      
    <md-autocomplete md-input-id="autocomplete" ...>
    1. Call blur() on the #autocomplete input element
    
    handleFormSubmit() {
      angular.element(document.querySelector('#autocomplete')).blur();
    }
    
    

提交回复
热议问题