Reputation: 1124
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
Upvotes: 0
Views: 856
Reputation: 1147
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.
Upvotes: 1