What is the best way to determine the selected option in a multi-select dropdown using AngularJS?

When using AngularJS to list options in a dropdown based on the ng-repeat value, how can I determine which options are selected in a multi-select dropdown like the one shown below:

<select custom-select ng-model="selectedEmp" data-container="body" multiple="">
    <option value="{{ emp._id }}" ng-repeat="emp in employeeList">
        {{ emp.first_name }} {{ emp.last_name }}
    </option>
</select>

Answer №1

https://docs.angularjs.org/api/ng/directive/select. The website explains that when the multiple attribute is set, it allows for multiple options to be selected and the selected values will be bound to the model as an array. Take a look at the example provided. It seems like this example demonstrates what you are trying to accomplish.

Answer №2

To accomplish this, you can use the following method:

HTML

<select custom-select ng-model="selectedEmp" data-container="body" multiple="">
    <option  ng-repeat="emp in names" ng-click="getSelected()">
        {{ emp.Name }} {{ emp.Country }}
    </option>
</select>

Controller

$scope.getSelected= function(){
  console.log($scope.selectedEmp)
}

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 Next.js environment variables not being recognized in an axios patch request

Struggling with passing environment variables in Axios patch request const axios = require("axios"); export const handleSubmit = async (formValue, uniquePageName) => { await axios .patch(process.env.INTERNAL_RETAILER_CONFIG_UPDATE, formVal ...

Is there a way to capture the click event of a dynamically generated row within a panel?

Could you please advise on how to capture the click event of a row that is generated within a panel? I have successfully captured events for rows generated on a page using the , but now I need assistance with capturing events from rows within a panel. I c ...

How to retrieve the total count of fields in AngularJS

Could you please specify the number of choices you would like to display? For example, if the user enters 10, then 10 choices will be displayed. I have been trying to implement this functionality, but it keeps resulting in a memory error. dynamicform.jsp ...

Generating conference itinerary - establishing agenda function (Sequelize)

const _ = require('lodash'); const Promise = require('bluebird'); const Sequelize = require('sequelize'); const ResourceNotFound = require('./errors').ResourceNotFound; const ResourceAccessDenied = require('./er ...

The HTML 5 video is not functioning properly on an iPad, and the layout of the iPad has black areas on the sides

Having some trouble with an HTML 5 project where the video plays on Chrome and Safari PC browsers but not on iPad. The task at hand is to get it working in portrait mode only, with the video playing when tapped/clicked. <!doctype html> <!--[if ...

Implementing server-side middleware for individual routes in Next.js

I am currently exploring options for executing code upon the initial page load of each request. My goal is to determine the domain of the request and redirect to a specific section of the website based on this information. One possibility I have considere ...

How can we wrap the Vuex store within a Vue plugin's install function?

I developed a plugin that utilizes Vuex for state management. // plugin.js import Vuex from "vuex"; import store from "./store.js"; export default { install(Vue, options) { const storeInstance = new Vuex.Store(store); Vue.pr ...

What exactly is the concept of lazily installing a dependency?

The website here contains information about the changes in Ember 2.11. Ember 2.11 now utilizes the ember-source module instead of the ember Bower package. In the upcoming Ember CLI 2.12 release, Bower will no longer be installed by default but will only ...

Is there a way to optimize Typescript compiler to avoid checking full classes and improve performance?

After experiencing slow Typescript compilation times, I decided to utilize generateTrace from https://github.com/microsoft/TypeScript/pull/40063 The trace revealed that a significant amount of time was spent comparing intricate classes with their subclass ...

Place the item at the top of the list before scrolling downwards

I'm struggling with what seems like it should be a simple task. My goal is to add new items to the top of a list and have the list scroll down to show the new item. So far, I've managed to create and add a new list item using this code snippet: ...

Showing a DIV multiple times with an additional text field in HTML and JS

As someone new to development, I am facing a requirement where I need to create a form with a dynamic field that can be added or removed using buttons. When the user clicks on the Add button, another field should appear, and when they click on Remove, the ...

Incorporate a collection of product titles along with their short forms in JavaScript/jQuery

Seeking guidance as a newcomer to JS. I have encountered the need for two different views in an application I am working on - one displaying full product names and the other showing only their abbreviations. Instead of hard-coding this information, I wish ...

Tips on retrieving the URL of a background image using "$event.target" to display in an Ionic modal

How can I display the clicked image in a modal? Implementation: <a ng-click="openModal($event)" ng-style="{'background-image': 'url(assets/img/img-01.jpg)'}"><img src="assets/alpha-4x3.png"></a> <a ng-click="openM ...

Creating an array of logos in ReactJS with the help of TailwindCSS

After seeing multiple implementations of this feature, I find myself struggling to search for a solution. I can only access the HTML and CSS through inspecting elements, wondering if others are facing the same issue as me. Typically, we aim to implement t ...

NG build error: Module parsing failed due to an unexpected token - no updates made

Two days ago, out of nowhere, we started encountering build errors during deployment using GitLab CI. No alterations have been made to the build scripts, and none of the versions of NPM, NG, or Angular have been modified. The same compilation commands cont ...

The custom filter in AngularJS fails to activate

Currently, I have a filter function that looks like this: 'use strict'; angular.module('app') .filter('fromNow', function() { return function(date) { return moment(date).fromNow(); } }); ...

Using Commas to Separate ChartJS Data Points

I am having trouble formatting numbers in a ChartJS graph to include commas. For example, my data points are currently displayed as 1032.05, 4334.75, 8482.46 but I need them to show as 1,032.05, 4,334.75, 8,482.46. If you would like to see the current cod ...

What are some ways to condense this Angular/TS code for improved performance and readability?

I am in need of assistance with refactoring a method called getBaseUrl(). This method assigns a specified string value to this.baseURL based on the input serviceType. getBaseUrl(serviceType: string, network?: string) { // Method logic to determine base ...

angularjs determining the appropriate time to utilize a directive

I have been delving into JavaScript and AngularJS for approximately a month now, but I still find myself unsure of when to use directives. For example: I am looking to display appointments in a table with the date as the table header. I envision having bu ...

Sort the activity based on $routeParams

I am structuring my routes in a way that is similar to Rails. Here are some example routes I have set up: $routeProvider.when('/articles', { controller: 'ArticlesCtrl', templateUrl: '/views/articles.html' }); $routeProvid ...