How can I capture the ng-click event in AngularJS when using bootstrap-select for styled select inputs?

After selecting an option, I am facing an issue where I cannot capture the ng-click event when a user chooses a value from the drop-down menu. This is because the select option has been altered by bootstrap-select, as the default select option does not support CSS styling so I had to resort to using bootstrap-select.

Check out the preview here

Here is the code:

<select class="form-control selectpicker col-md-6 col-md-offset-3 input-question input-text v2-mod" 
           id="{{question.name}}"
           name="birthyear"
           required
           ng-model="answers.userdata[question.name]"
           ng-class="{'input-error': formValidation && questionForm.birthyear.$error.required}" >
               <option value="{{option.value}}" 
                       ng-click="next('stage5')" 
                       ng-repeat="(key, option) in question.options">
                                            {{option.label}}
               </option>
</select>

The code is affected after being modified with Bootstrap-select:

See the updated preview here

Answer №1

Utilizing ng-options and ng-change as default in Angular is recommended, it's more efficient than using ng-repeat on options.

Important: Remember to apply ng-change to the select element and not the individual option elements.

var app = angular.module("app", []);

app.controller("ctrl", function($scope) {

    $scope.data = [
        {name: "a"},
        {name: "b"},       
    ]
    
    $scope.get = function(value){
    
      console.log(value)
      
    }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">


    <select ng-model="form.select" ng-options="item as item.name for item in data" ng-change="get(form.select)"></select>

</div>

Answer №2

To utilize the ng-change event and connect it to ng-model, follow these steps:

var app = angular.module("app", []);

app.controller("ctrl", function($scope) {
  $scope.changedValue = function(item) {
    console.log(item);
  }
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet" />
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.css" rel="stylesheet" />

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <select class="selectpicker" ng-model="item" ng-change="changedValue(item)">
  <option value="">None</option>
  <option value="Mustard">Mustard</option>
  <option value="Ketchup">Ketchup</option>
  <option value="Relish">Relish</option>
  </select>

</div>

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

Issues with jqGrid treeGrid expanding and collapsing functionality not functioning as expected

I'm having some trouble setting up a treeGrid using jqGrid 4.2.1. After working on it, the visual aspect looks fine, but I'm encountering an issue with expanding and collapsing nodes. The icon toggles properly, but the groups remain visible. Her ...

Displaying modal popups limited to one per session

Is there a way to display this Dialog Popup only once per session? I have looked into using cookies for configuration, but haven't been able to implement it in this scenario: $(document).ready(function() { ShowDialog(true); e. ...

Spacing between the rows of a table

I am seeking to achieve a unique visual style for the rows in my table, similar to the one shown below. I attempted to use CSS border properties in the code snippet provided but desire more spacing between each row with a thin border and sharp edges. .dat ...

putting a division element above a image carousel

Is there a way to overlay a div tag on top of a slideshow, similar to the one shown in this link? In the example provided, a div with the title "DISCOVER THE JOY OF COOKING" is positioned over a slideshow. Any tips on how I can achieve this effect? ...

What are the alternatives to invoking a server-side C# method from JavaScript without using a WebMethod or UpdatePanel?

I am looking for alternatives to using an update panel. The common WebMethod approach is causing errors with the code below: private string currentHtml() { StringWriter str_wrt = new StringWriter(); HtmlTextWriter html_wrt = new Htm ...

"Utilizing jQuery to asynchronously make an AJAX request with a setTimeout

Question about SetTimeout: Why does setTimeout(fn, 0) sometimes help? After reviewing the jQuery 1.8 source code, WHAT is the reason behind using setTimeout with a delay of 0 ms ? (instead of simply executing the callback immediately ?) Link to the ...

Toggle the checkbox to update the count

I am trying to implement a feature where the user can check a maximum of 4 checkboxes. I have managed to achieve this functionality in the code below. When the user unchecks a box, the count should decrease by one and they should be able to check another c ...

jQuery can be used to mask phone numbers with three input fields

I am looking to implement phone field masking using jQuery or JavaScript. I have tried a few jQuery mask plugins, but I am still searching for a better solution. Specifically, I need three input fields for the USA (it is a requirement). The inputs needed ...

Ways to vertically center an absolutely positioned element

In the code snippet below, there is a .square-container that is absolutely positioned and contains a square. The goal is to vertically center the .square-container within its parent div. .container { position: relative; background-color: blue; } ...

Tips for creating square corner images within a bootstrap card group

How can I create a square image gallery using Bootstrap 4 cards? <div class="card-group flex-wrap"> <div class="card border-dark"> <img class="card-img-top" src="h ...

Incorporate jQuery into your MVC view by including Html.BeginForm

Using Ajax, I have info boxes that are loaded by calling a function in my controller and generating the HTML dynamically. Now, I want to implement an edit functionality for these info boxes where the static text will be replaced with a form. I am familiar ...

Tips for generating sub rows in AngularJS:

I have recently started working with Angular and I am trying to create a table that displays parent rows with child rows. When the user clicks on a parent row, I want to dynamically generate the corresponding child rows (indicated by a carrot sign). I woul ...

Extract latitude and longitude coordinates from a dataset by applying a rectangular filter

I have developed a program that extracts information from a JSON object and showcases it on a webpage, specifically focusing on Public Bike Stations. The JSON object includes the latitude and longitude of each station, making it easy to locate them. My cu ...

unable to display data through the web service

The functionality of this code is correct, but it seems to not be displaying records. When the record is retrieved from the file and shown in an alert, everything works fine. $j().ready(function(){ var result =$j.ajax({ ...

Creating a custom function in JavaScript to interact with the `windows.external` object specifically for use

In my current project, I am facing an issue with JavaScript file compatibility across different browsers. Specifically, I have a JavaScript file that calls an external function (from a separate file) using windows.external, like this: windows.external.se ...

Refreshing div content on selection change using PHP ajax with Jquery

Here is my code for the portfolio.php file <select name="portfolio" id="portfolio_dropdown" class="service-dropdown"> <?php foreach($years as $year){ ?> <option value="<?php echo $year[&apo ...

I am sending the session data through PHP tags, and then attempting to dynamically hide and show a div element upon page load using jQuery, depending on the conditions specified

By passing the user role from PHP using $session['role'] tags, I attempted to hide and show a specific div on page load based on the condition if(!sessionRole=='admin') using jQuery. <script src="https://code.jquery.com/jquery-3.3 ...

Using PHP and AJAX for deletion will only work on the initial row

I created a basic PHP blog platform. On the Add Post page, users can input a title and body text for their posts. These posts are displayed on the Posts page, where users also have the option to delete them. However, I encountered an issue where only the ...

Flag a specific property on a directive scope as needing an update

Recently, I made the switch to using AngularJS in a web application that was previously mostly jQuery based. One of the components in this app utilizes jQuery UI's draggable feature, which relies on the CSS left property for positioning objects during ...

AngularJS: Incorporating multiple data components into a singular ng-model

Can a ng-model have multiple data values assigned to it? For example: <select ng-model="data1,data2" ng-change="changedValue(data1,data2)" If not, what are some common ways people address this problem? ...