Mastering the use of event callbacks in AngularStrap typeahead

I am currently utilizing an AngularStrap typeahead feature and I am in need of a callback function that will be executed when a user selects an item. As per the guidelines provided in the documentation, there is an option called onSelect which requires a function to be passed, with the statement:

If specified, this function will be triggered upon selection of an item.

...and...

Options can be set using data attributes on the directive or as an object hash for configuring the service. When using data attributes, add the option name after data-, for example, data-animation=""

Therefore, I attempted to implement it in the following manner:

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

I also included the onSelect() method within my controller:

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

Despite this setup, the callback function does not seem to trigger. Is there something I am overlooking?


[edit] dfsq mentioned that it should actually be bs-on-select according to the library's sources. I have tested this alternative, however, the event only fires once. I have created this Plunker to demonstrate my problem; ideally, the "Number of selection events" should increment with each selection, but it remains at 1.

Answer №1

It appears that the solution was provided in the comments by @Vanojx1, suggesting the correct code should be:

bs-on-select="onSelect"

I have made the necessary update to the Plunker with this change, and it's functioning well (though it only triggers the method after the input loses focus).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with your computed includes not working in Vue.js? Unsure of how to fix it?

How come when I use "includes" in Vue with my v-model, Vue.js logs an error? However, if I write (.includes("blabla)), everything works fine. What could be the issue? Also, how can I return the entire array if the (if) condition will work? For example, ...

Methods for transferring data from an AJAX function

I have a database containing various links that I would like to retrieve and store within an array. I attempted to achieve this using the following code: var amz = new Array(); function CreateAmazonArray() { $.ajax({ url: "php/amazon_affilia ...

Tips for converting JSON object values to a string using JavaScript

Consider the following JSON object: { "id": 1, "name": "Name", "surname": "Surname", "number": "111111111" } Is there a way to transform this JSON object into a string that formats it as follows using JavaScript? Name Surname 111111111 ...

`When you extend a $resource object, it effectively removes the prototype from the

Here's the current code snippet I'm working with: // Factory angular.factory('Student', function() { var defaults = { StudentId: null }; var Student = function(params) { angular.extend(this, angular.copy(de ...

Combining audio and video streams in Node.js

I'm currently developing a YouTube video downloader using the ytdl-core library. However, I've encountered an issue where it cannot fetch high quality videos with audio because they are stored in separate files on YouTube. My goal is to download ...

Personalized HTML selection list

When a select option is too long, it stretches the container to accommodate its length. I have created a truncate function to limit the display to 20 characters and add ellipses to prevent this stretching. I have been searching for a way to show the entir ...

Determining the function responsible for triggering an ajax error

I've implemented a global error handler for handling ajax calls, where I capture the error details, send them to a PHP script, and then log the outcomes: $(document).ajaxError(function(event, request, settings){ //get the error and log the resul ...

Challenge with Combining jQueryUI Tabs and AngularJS

I am encountering an issue with dynamically generating tabs using jQuery UI and AngularJS. I have created a directive to call the Tabs(jQuery-UI) initialization on a div tag that contains ul > li> a elements generated with ng-repeat. The problem se ...

What steps should I take to arrange this information in this particular format?

Exploring nested comments data and structuring it effectively. I am seeking assistance in optimizing a function that can transform an array into a structured format, as depicted below. raw data const data = [ { id: 1, text : "Hell ...

Puppeteer is unable to detect the node.js handlebars helpers

I'm utilizing puppeteer in NodeJs to generate a PDF file. I use handlebars to render the template and pass variables during compilation for handlebars to access them. Here is the current code snippet: const browser = await puppeteer.launch({ he ...

"Troubleshoot: Issue with React's useState hook not correctly updating state within an

I'm running into an issue with updating state using the useState hook in a React functional component. The problem arises when I have an async function (getPalettesPosition) that fetches data based on the current state (paletteFilters). Despite confir ...

Leveraging Angular's capability to import files directly from the assets

I recently installed a library via npm and made some modifications to one of the modules. python.js If I delete the node_modules folder and run npm install, I am concerned that I will lose my changes. Is there a way to preserve these modifications by mov ...

Unlocking the Secret: Effortless Transference of an AngularJS Scope Variable from Directive to Controller

Is there a simpler way to transfer an AngularJS scope variable from a directive to a controller? I've come across some complicated examples, but can't we directly access a controller from a directive and update one of its scope variables? ...

Unable to detect the click event on Bootstrap accordion

I am currently utilizing Bootstrap to populate a table with data and then attempting to capture an accordion show event. My intention is to extract the div id in order to make an ajax call for more information on the respective item. Unfortunately, I have ...

I encountered a 429 error while trying to make a request to the Spotify API

I recently encountered an issue with my website where I am trying to retrieve and display my playlists. Everything was working fine yesterday, but this morning I started getting error 429 every time I tried to retrieve data from the api, even though I have ...

Can I determine if onSnapshot() has detected any updates prior to fetching the data?

While navigating to a page, I pass parameters like "Discounts" and display that number in the render(). However, I call onSnapshot() right at the beginning of navigating to that class. My goal is to initially display only the value of the parameter and the ...

display the text content of the chosen option on various div elements

I created a subscription form that includes a category dropdown select field. The selected option's text value needs to appear 4 times within the form. It's all part of a single form. <select name="catid" onchange="copy()" id="catid" class="i ...

Unusual glitch involving onChange method in React JS

Encountered a peculiar bug with an autocomplete form. I implemented an onChange function for the form that can access two arguments, the "event" and the "value". The function is structured like this: handleChange(event, value){ const newElement = ...

What is the best way to maintain the toggleClass event state once an ajax call has been made?

If I have the HTML code below <div class="row"> <div class="link grey"> <a href="#">Link</a> </div> </div> <div class="row"> <div class="link"> <a href="#">Link</a> </div> & ...

The refusal to run JavaScript stemmed from an empty MIME type, resulting from the combination of nodeJS, express, engintron, and nginx

Currently, I'm struggling with a frustrating issue that's making me want to pull my hair out. So, here's some important information you need to be aware of: My application is built using node.js (version 16.13.1) with express and nginx man ...