简体   繁体   中英

when md-autocomplete pressed call to function

I'm using Angularjs for my project. I need to write a function that called when the user just clicks on the autocomplete. (without typing any character) what is the best way to do it? I tried this:

$('.to').on('autocompleteSelect', function(event, node) {
                debugger;
                console.log("sss")
                });

but this is doesnt work

Thanks

You can use the ng-focus directive in AngularJS to evaluate on expression on input focus. Using md-autocomplete , this would look something like this:

  <md-autocomplete
      ng-focus="ctrl.autocompleteGotFocus()"
      ng-disabled="ctrl.isDisabled"
      md-no-cache="ctrl.noCache"
      md-selected-item="ctrl.selectedItem"
      md-search-text-change="ctrl.searchTextChange(ctrl.searchText)"
      md-search-text="ctrl.searchText"
      md-selected-item-change="ctrl.selectedItemChange(item)"
      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 states matching "{{ctrl.searchText}}" were found.
      <a ng-click="ctrl.newState(ctrl.searchText)">Create a new one!</a>
    </md-not-found>
  </md-autocomplete>

Note that this example is copied directly from the angular-material docs, here: https://material.angularjs.org/latest/demo/autocomplete

Here is a an example plunker: https://plnkr.co/edit/M5itKN0ob2PDcTgIjtVe

You'll see in the console that a message is logged from the autocompleteGotFocus function when the autocomplete first gets focused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM