Search code examples
javascriptangularjsangular-strap

AngularStrap typeahead, how to use event callbacks


I'm using an AngularStrap typeahead and need a callback, when an items gets selected by the user. According to the documentation, there is an onSelect option, which can be supplied with a function, and:

If provided, this function will be invoked when an item is selected.

… and …

Options can be passed via data-attributes on the directive or as an object hash to configure the service. For data attributes, append the option name to data-, as in data-animation="".

So I tried using it as follows:

<input type="text" 
       class="form-control" 
       ng-model="selection" 
       bs-options="item for item in items" 
       bs-typeahead 
       data-on-select="onSelect">

And provide the onSelect() method within my controller:

$scope.onSelect = function() {
    console.log('this never gets called :(');
};

However, the callback is never invoked. Anything I'm missing here?


[edit] dfsq pointed out that it should be bs-on-select, regarding to the library’s sources. I just tried this variant, however the event only gets triggered once. I created this Plunker to illustrate my issue; the "Number of selection events" should naturally be incremented with each selection, however it remains 1.


Solution

  • Looks like this was answered in the comments by @Vanojx1 and that it should be:

    bs-on-select="onSelect"
    

    I've updated the Plunker with this change and it works (however, it doesn't call the method until the input loses focus).