How to Retrieve the Selected Value from an AngularJS UI-Select

At the moment, I am facing an issue with multiple dropdown directives that are using the same array of values. Whenever a selection is made in one dropdown, it updates the selected value in all other dropdowns. How can I ensure that each dropdown has a unique selection?

My current workaround involves setting ng-model="ctrl.ngModel", but this prevents me from accessing the selected value in the dropdown.

dropdown.html

<div>
  <ui-select name='' ng-model="ctrl.ngModel.selected" theme="bootstrap" class='expiry admin-expiry'
    append-to-body='true'
    reset-search-input='true'
    on-select="ctrl.update()">
    <ui-select-match placeholder="">{{$select.selected.value}}</ui-select-match>
    <ui-select-choices repeat="choice in ctrl.choices | filter: $select.search"
      position='down'>
      <span ng-bind-html="ctrl.trustAsHtml(choice.value)"></span>
    </ui-select-choices>
  </ui-select>
</div>

controller.js

  function ColDropdownDirective() {
    return {
      restrict: 'EA',
      scope: {
        ngModel: '=',
        choices: '='
      },
      controller: ['FxoUtils', function(_) {
        var ctrl = this;
        this.trustAsHtml = function(value) {
          return _.trustAsHtml(value);
        };

        this.update = function() {
          console.log(ctrl.ngModel.selected);
        };
      }],
      controllerAs: 'ctrl',
      bindToController: true,
      templateUrl: 'js/fxo/admin/thresholdconfig/common/common.col.dropdown.html'
    };
  }

Answer №1

To create three unique copies of your choices and store them in an array, you can then access these selections in your template by using choice in ctrl.choicesArr[0], [1], or [2]. Make sure to assign different ng-models for each dropdown to represent the selected value.

function CustomDropdownDirective() {
    return {
        restrict: 'EA',
        scope: {
            ngModel: '=',
            choices: '='
        },
        controller: ['Utils', function(_) {
            var ctrl = this;
            this.choicesArr = [
                angular.copy(this.choices),
                angular.copy(this.choices),
                angular.copy(this.choices),
            ];
            this.trustAsHtml = function(value) {
                return _.trustAsHtml(value);
            };

            this.update = function() {
                console.log(ctrl.ngModel.selected);
            };
        }],
        controllerAs: 'ctrl',
        bindToController: true,
        templateUrl: 'js/custom/dropdown/template.html'
    };
}

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

Is the image overlay experiencing a flickering issue?

Last time, none of the solutions seemed to work quite right with the project, so let me try asking the question in a slightly different way this time. Basically, I have an image that, when someone hovers their mouse cursor over it, a div appears (containi ...

"Why Using the Spread Operator to Copy Arrays in React does not yield the expected

While working with the state in react js, I encountered an issue where a variable named copy was being updated unexpectedly. The scenario involves copying the contents of a fake array called jObj using the spread operator and then modifying the original ar ...

Verify whether an object possesses all the attributes of a class in TypeScript

Within my typescript code, I have a class called abc: export class ABC{ public a : any; public b : any; public c? : any; public d? : any; } In one of my functions, I receive an input which is represented as data:any. My goal is to verify i ...

Sending a document through the input field with React Hook Form

In my application, I have implemented a file input to submit a file and send it to a firebase storage bucket. To achieve this functionality, I am utilizing the react-hook-form library. However, I encountered an issue - I wanted the file to be uploaded with ...

Is there a way to transform a personalized jquery event listener into an angularjs listener?

A custom event listener was created using jQuery for an angularjs component. The event is triggered by a non-angularjs library. $( "#myDiv" ).on( "CornerstoneImageRendered", function(e) { // buisness logic }); The element myDiv is a div that belongs t ...

The $().bind function will not function properly unless the document is fully loaded

Do I need to include $(document).ready() with $().bind? Here is the HTML portion: <head> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> <script type="text/javascript" src=&quo ...

The computed style of a node in Chrome returns as null

In my javascript code, I have the following line: if(document.defaultView && document.defaultView.getComputedStyle){ strValue = document.defaultView.getComputedStyle(oElm).getPropertyValue(strCssRule); } // ... This code is used to retrieve a ...

Error: The term "Particles" has not been defined

I'm attempting to integrate code from a website into my project, but encountered an error when the particles failed to run after adding it. I downloaded and installed particle.js from "https://github.com/marcbruederlin/particles.js/issues" for this pu ...

React window resizing actionsWould you like some more information on this

I am attempting to create a resize function that will retrieve the window width and dynamically display it using react. Here is my current code: class Welcome extends React.Component { constructor() { super(); this.state = { ...

Launching an Electron Window and Recording the Closing Event

I am currently developing a Desktop Application using Electron. I want to implement a feature where clicking on a button in the main window (index.html) will open a different window. After researching, I discovered the BrowserWindow from the Electron API. ...

Implementing a bloom pass in ThreeJS can alter the transparency of a canvas

IMPACT OF BLOOM EFFECT ON TRANSPARENCY Currently, my renderer setup looks like this: renderer = new THREE.WebGLRenderer( { antialias: true, preserveDrawingBuffer:true, alpha:true } ); For implementing the bloom pass in post-processing: var renderPass = ...

Preventing scrolling in one div while focusing on another div

I am currently in the process of creating a website that functions as a single page site. The main feature of the site is a masonry grid of images. When a user clicks on an item, a detailed panel slides in from the left. Once the panel is closed, it slide ...

Engaging with Electron through an HTML file

Forgive my lack of experience, but I'm diving into the world of Electron and feeling a bit overwhelmed. Here's a snapshot of what I've got so far in my project: package.json: ... "main": "main.js", "scripts": { "start": "electron ." } . ...

Verifying the presence of a value within an SQL table

I am currently working on developing a bot that requires me to save the commandname and commandreply in a database. Right now, I am using mySQL Workbench for this task. My goal is to verify if the commandname provided by the user already exists in the tab ...

A guide on how to automatically preselect a RadioGroup option in Material-UI

When a user selects an option from the MCQ Select using RadioGroup in my code and submits it, they should be able to return later and see the option they selected highlighted, similar to how Google Forms allows users to review their selections. Below is t ...

Transfer photos and videos to an external server using Javascript with Meteor framework

I currently have a meteor application hosted on Digital Ocean. I am considering setting up a dedicated server to store all images and videos separately from the site itself. Whenever a user uploads new media, it will be saved to this separate server. Does ...

Tips for activating this effect even when the window is resized during page scrolling

There's a code snippet that enables the header to become unfixed when a specific div reaches the top of the screen and then scrolls with the rest of the content. While this solution works perfectly, I encountered a problem where the calculations for ...

Personalizing Vuetify Calendar: Modifying the default Day Label Header

Exploring the Vuetify calendar for appointment scheduling has been a unique experience. Despite attempting to modify the default header using slots, I have not achieved the desired outcome. My goal is to customize it to resemble the design showcased in thi ...

Ways to prevent mouse selection in an input field without disabling or making it read-only

Imagine this scenario: <input type="text"> The task at hand is to prevent text selection within the <input> field. ...

Issues with executing the success function in AngularJS `$http` requests

Has anyone encountered an issue with receiving HTTP response from a Django local server that returns a JSON output? The JSON response looks like this: [{"fields": {"DOB": "2015-05-08", "image": "media/jainsubh_1394437734_6.png", "text_file": "my_Txt_File ...