AngularJS: Populating the dropdown menu with selected data in ui-select

I am currently utilizing Angular.js version 1.3.4 and implementing ui-select within my project.

My challenge lies in the process of binding data back to the ui-select component and a textbox after saving the information to the database successfully. Even after modifying the JSON data retrieved from the database, I encounter difficulties with this task.

The JSON structure obtained from the database:

{
   "input1":{
      "set1":{
         "source":"uat1",
         "value":"value1"
      },
      "set2":{
         "source":"uat",
         "value":"value2",
         "options":[
            {
               "name":"option1"
            },
            {
               "name":"option2"
            }
         ]
      }
   },
   "input2":{
      "set3":{
         "source":"uat1",
         "value":"value3"
      },
      "set4":{
         "source":"uat",
         "value":"value4",
         "options":[
            {
               "name":"option3"
            },
            {
               "name":"option4"
            }
         ]
      }
   }
}

Below is an excerpt from my directive attempting to bind back the data:

 <div class="form-group" ng-repeat="(name, set) in sets">
    <label>{{name}}</label>
    <ui-select name="{{inputName + $index}}" ng-model="set.value" theme="bootstrap" ng-if="set.source == 'uat'">
        <ui-select-match placeholder="Set">{{set.value.name}}</ui-select-match>
        <ui-select-choices repeat="ds in set.options | filter: $select.search track by $index">
            <div ng-bind-html="ds.name | highlight: $select.search"></div>       
        </ui-select-choices>
    </ui-select>
    <input type="text" ng-if="set.source == 'uat1'" name="{{inputName + $index}}" ng-model="set.value" />
</div>

In the above code snippet, while the options display correctly in the ui-select, the issue arises with the displayed values. The selected values do not appear in the ui-select, and the textbox remains empty as well.

Based on the model 'ng-model = 'set.value' assigned to each control, I expected the binding to function properly, but it seems something is missing in the JSON structure. Any guidance on identifying the missing element would be greatly appreciated.

Thank you.

Answer №1

When working with AngularJS, the ui-select module includes a directive called ui-select-match that highlights the selected value and displays it in the input field. If you are seeing incorrect data (set.value.name), make sure to display the selected value using $select.selected.name instead.

<ui-select-match placeholder="Set">{{$select.selected.name}}</ui-select-match>

If your ui-select-choices are objects, setting a default value from the controller requires that the specified value exists within the options as an object. To assign a default value to the ui-select, ensure the correct value is assigned to the ng-model set.value.

"value": {
    "name":"option1"
}

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

Design a table within an mdDialog that allows for editing of data presented in JSON format

I'm attempting to implement a dialog using JSON data. $scope.showAttributeData = function(data) { $scope.feature = data console.log($scope.feature) var that = this; var useFullScreen = ($mdMedia('sm') ...

The StreamReader in .NET is unable to read the stream that was sent from Internet Explorer 10 using Angular 1.5's $http.post method with JSON data

Initiate a call: $http.post(serviceName, data, { tracker: tracker }); The Content-Type is set to application/json;charset=utf-8 as per IE10 standards Retrieve and read the response: StreamReader reader = new StreamReader(rawJSON); The length of the St ...

The placeholder text feature for Google custom search is ineffective when using the Edge browser

After adding the following code to the end of the Google custom search script, I encountered an issue: window.onload = function(){ document.getElementById('gsc-i-id1').placeholder = 'Search'; }; Although this code successfully added ...

How to Pause or Temporarily Halt in Jquery?

Looking to lift the object up, wait 1000ms, and then hide it. I found this snippet of code: $("#test").animate({"top":"-=80px"},1500) .animate({"top":"-=0px"},1000) .animate({"opacity":"0"},500); However, using ".animate({"to ...

Is there a way to customize the color of an element within CardHeader in MUI?

Is there a way to customize the colors of {note.title} and {note.category}? I have attempted to do so by creating a theme: const theme = createTheme ({ palette: { category: { color: blue } } }) Unfortunately, this appro ...

The expected number of calls for the callback function was not met

Our employee webpage is designed to make Ajax calls to the node.js web server in a continuous loop. The problem arises when the callback UpdateTeamArr is expected to be triggered multiple times based on the loop max and the number of department options a ...

Having trouble with data not being refreshed in AngularJS form

One of the issues I encountered involves an edit button in a form. When a user clicks on the edit button, makes changes to the form, and saves it, the updates are successfully applied. However, upon reloading the page, the changes are not reflected. Belo ...

When using Vue.js, styling SVGs with CSS proves difficult as it applies inline styles in the DOM rather than using class names

Recently, I tried to update the appearance of some SVG images using CSS, but to my surprise, nothing changed. Upon inspecting the page, I noticed that the styles were located under the 'element.style' tag, which may explain why my attempts were u ...

The function IdleProvider.windowInterrupt does not exist

I'm currently attempting to set up the most basic implementation of ng-Idle in node.js on my development environment. To achieve this, I have utilized the minimal sample provided at this link, and have integrated it into a previously functional minima ...

Click to open the file browser by using the onclick event in Material-table actions

Currently, I am working with a Material table component from "@material-table/core" My goal is to implement an action that enables users to upload a file within this table. I am facing some challenges on how to trigger the file browser when the ...

eliminate the common elements between two arrays in typescript/javascript

I have two lists of objects, each containing two fields: let users1 = [{ name: 'barney', uuid: 'uuid3'}, { name: 'barney', uuid: 'uuid1'}, { name: 'barney', uuid: 'uuid2 ...

How can the callback from C++ be successfully installed in the Javaobject window within QtWebkit?

I have successfully implemented HTML-JS to call a C++ method using QtWebkit. Now, I want to send a callback from the C++ method to the JavaScript window. How can I achieve this? Below is my code snippet. #include <QtGui/QApplication> #include <Q ...

Why is it that masonry is typically arranged in a single column with overlapping

I have been using the appended function in my code to add elements to a masonry instance that has already been initialized. However, I am facing an issue where all the tiles are being laid out in a single column and some of them are overlapping each oth ...

What is the best way to use appendChild within an AJAX get function to manipulate the DOM

I've been working on a code function where I try to append a list item (li) into the HTML received in 'msg', but it's not functioning properly. Any ideas why? function handleFileSelect(evt) { var files = evt.target.files; $(&ap ...

Verify whether the selection has changed within the OnClick event

Take note: The answer marked as the solution addresses the issues presented in the Title. However, I was able to resolve my underlying issue with type ahead dropdowns by switching to IE8. I am looking to trigger a postback ("this.form.submit()") when a ne ...

Anyone have a solid example of STLLoader.js code to kickstart a project in three.js? Looking for a clean and reliable snippet

I am looking to add the STLLoader.js from three.js to my web application, but I am struggling to find clean and easy-to-understand code examples on how to get started. While the official three.js website offers a demonstration example for the STLLoader (h ...

Encountering a problem with the mouseup event

Hey there, I've got this code up and running smoothly but I'm looking to make a tweak. Is there a way to have the script halt on the mouse up event? Currently, it displays coordinates as you drag over an image, but I'd like it to only show ...

How can we show a varying number of textfields based on user selection using hooks and updating values dynamically through onChange events?

Currently, I am in the process of setting up a form that includes a Material UI select field ranging from 1 to 50. My challenge is how to dynamically display multiple textfields for "First Name and Last Name" using Hooks each time the user selects a number ...

JavaScript's `setAttribute` function appears to be malfunctioning when used in conjunction

I am currently working in ORMB and have come across an input element that looks like this <input id="charVal" class="oraInput" oraField="charVal"> I've been trying to dynamically add an oraSearch attribute using JavaScript, but it doesn't ...

Switching from cURL to Ajax resulted in receiving a 400 Error code

My goal is to save specific URLs into a jsonbox, but I keep encountering Error 400. The curl command provided in the documentation is as follows: curl -X POST 'https://jsonbox.io/demobox_6d9e326c183fde7b' \ -H 'content-type: applic ...