Having difficulty with Angular's ng-options feature in a Select element

I'm currently tackling an issue with using ng-options to iterate through an array of objects and display specific properties in a select element.

Upon querying the api/admin endpoint, I receive JSON data containing details of all users holding the admin role. Let me break down how the data is structured.

[
 {
 "Roles": [
          {
           "Id": 149,
           "UserId": 1807,
           "Name": "Administrator",
           "RoleGroup": "System"
           },
           {
           "Id": 148,
           "UserId": 1807,
           "Name": "Administrator",
           "RoleGroup": "System"
           },
           {
           "Id": 150,
           "UserId": 1807,
           "Name": "Lead",
           "RoleGroup": "System"
           }
         ],
 "Id": 1807,
 "UserName": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8929c979db88f9d9a8b918c9dd69b97">[email protected]</a>",
 "FirstName": " John",
 "LastName": "Doe",
 "Email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="08626c676d487f6d6a7b617c6d266b65">[email protected]</a>"
 }
]

The goal is to create a <select> element that shows the concatenated values of the FirstName and LastName properties.

To retrieve the data from my API, I use the following query: $scope.admin = Api.admin.query

Subsequently, within my HTML markup, it is implemented like this:

<label>Admin*</label>
    <select class="form-field"
        ng-options="a.Id as a.FirstName + a.LastName for a in admin" required>
        <option value="">Select an Admin</option>
    </select>

I am seeking guidance on what steps might be missing to achieve the desired functionality. Any assistance would be greatly valued!

Answer №1

Make sure to add an ng-model attribute in the <select> tag to store the selected value

  <label>Administrator*</label>
  <select class="form-field" ng-model="selectedValue" ng-options="a.Id as a.FirstName + ' ' + a.LastName for a in admin" required>
     <option value="">Choose an Administrator</option>
  </select>

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

Disabling the search function when it is clicked (before any actions are taken)

I've recently implemented a search form on my website, where the search field opens up upon clicking the search icon.https://i.stack.imgur.com/UeErx.png To make the search icon clickable, I disabled the search function. var $searchBtn = $search.find ...

Transforming a string field into a property object through deserialization

Assuming I have a serialized Json object of type MyClass like this: {"DurationField":"3 Months", *OtherProperties*} My goal is to deserialize this Json into the following class structure: public class MyClass{ *OTHER PROPERTIES* p ...

Looping through JSON objects using for loop

I am trying to implement a for loop with the following json data. The console.log function is working properly, but when I use it in the javascript function, it does not work as expected. I want the for loop to be functional within the javascript function ...

Tips for expanding an md-nav-item in Angular Material

Need help on stretching the md-nav-item element illustration 1 ------------------------------------------------ | ITEM1 | ITEM 2 | ------------------------------------------------ illustration 2 ------------------------------------------------ | ...

Utilizing Date Model Binding in ASP.NET Core Framework

While working on an ASP.NET Core Web API, I encountered an issue with binding DateTime values. Specifically, I have two properties - minimumDate and maximumDate - for filtering a certain resource. These properties are part of a Filtering object that is po ...

What is the proper way to update a dropdown value in a React JS component?

Can you please guide me on how to assign a value in a dropdown in react js? I am retrieving the dropdown data after a delay of 3000 milliseconds and then I need to set a value in the dropdown. const App = ({ children }) => { const val = "ax"; const ...

What is the best way to utilize regex to replace two elements simultaneously?

I am facing a challenge with manipulating a string of characters by adding span tags to highlight specific words and change their color. While I have successfully implemented the changes for one pattern using regex, I'm struggling to do the same for a ...

How can I implement user account functionality with only JavaScript in an Angular frontend and Node.js/Express server?

Many resources I've come across utilize php or a similar language. With an Angular frontend and Node/express server code in place, but no backend yet, I'm uncertain about how to approach user sign-up. ...

The versioning of Ionic for Android experiences a setback

I am facing an issue with the version numbers in the Google Play store. In the past, my app versions were formatted as follows: Version code 10172 (5 digits) Version name 1.1.72 However, after switching build machines, I unknowingly uploaded a version ...

Why is my bindingResult returning null even after the Spring Validator detects duplicates in my code?

Below is the registration controller method for Sensor, kept simple: @PostMapping("/registration") public ResponseEntity<HttpStatus> create(@RequestBody @Valid SensorDTO sensorDTO, Bindin ...

Upgrading from synchronous $.ajax() to AngularJS $http or vanilla JavaScript (XHR) for asynchronous calls

Presented below is a function that I am currently working with: function getDataAvailablity() { var isDataAvailable; $.ajax({ url: 'http://someurl/data.json', async: false, dataType: json }).success(function() ...

Looking to create a format for displaying short comic strips in a grid pattern

I am struggling to create an image grid for my small comics. I want to have 2 columns and 3 rows, but all the divs end up in the same place. I tried using display: flex, but it didn't work as expected. Additionally, I want the grid to be responsive, b ...

Issues arise when attempting to redirect a user to an AngularJS form and having

Can anyone guide me on how to automatically fill an edit form upon redirection in AngularJS? Here's the situation: I have a list of products. When I click on edit product, I should be directed to the edit form (using AngularJS routing), and the form ...

Implementing JavaScript functionality based on a specific body class

Is there a way to execute this script only on a specific page with a particular body class? For example, if the page has <body class="category-type-plp"> How can I target my script to work specifically for "category-type-plp"? plpSpaceRemove: fun ...

Troubleshooting issue with multer code failing to save files in designated directory within nodejs server when using reactjs frontend

I'm facing an issue while trying to upload a photo into my server folder. Even though the submission process adds the picture to my database, it does not display the image in my server folder. The frontend of my application is built using React JS, an ...

Transferring Information Between Vue Components

In my project, I have implemented 3 Vue components to work together seamlessly. Component A is responsible for listening to an event triggered by a user clicking on HTML text and storing the data into a variable. Component B utilizes this data to make an A ...

Incorporate a widget with dynamic height adjustment

We are currently working on creating a widget that can be easily embedded by third-party websites. Our goal is to have the widget automatically adjust its height through the embed script. Initially, we considered using an IFrame generated by our JavaScrip ...

Experiencing a 400 error while transitioning from ajax to $http?

I am facing an issue while trying to make a GET request using $http instead of ajax. The call functions perfectly with ajax, but when attempting the same with $http, I encounter a 400 (Bad Request) error. I have referenced the Angular documentation and bel ...

Implementing pagination for images offers a user-friendly browsing experience

My friend and I are in the process of creating a website that displays images from two directories in chronological order. The image name serves as a timestamp, and we generate a JSON object using PHP with the code below: <?php $files = array(); $dir ...

Python: convert a list generator to be serializable in JSON format

Is there a way to concatenate a large list of JSON files into a single huge JSON array? I have 5000 files and 550,000 list items in total. Initially, I attempted using jq, but it seems that jq -s is not suitable for handling such a large input size. jq - ...