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

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

I have md-autocomplete:



        
5条回答
  •  悲&欢浪女
    2021-02-13 05:34

    I think this solution is better because:

    • it uses a directive instead of the controller.

    • it is simpler than the other given directive solution.

    Javascript

    app.directive('closeOnEnter', function($compile) {
          return {
             restrict: 'A',
             require: 'mdAutocomplete',
             link: function(scope, element) {
                element.on('keydown keypress', function($event) {
                   // 13: Enter
                   if ($event.keyCode == 13) {
                      var eAcInput = this.getElementsByTagName('input')[0];
                      eAcInput.blur();
                   }
                });
             },
          };
    });
    

    HTML

    
    

提交回复
热议问题