I am unable to configure the value using NG-Options

Currently, I am facing an issue with loading a select box using the ng-options directive.

The problem is that I am unable to have the options display values ranging from 23-26, instead of 0-3. The values should match the "eigenschap_id" that I pass through.

I'm wondering where the mistake lies in my code. Below is the snippet I am working with:

Controller:

$scope.eigenschappen.game = categories.Game;

JSON string / object:

[{"cat_id":6,"cat_name":"Game","eigenschap_name":"RPG","eigenschap_id":23},{"cat_id":6,"cat_name":"Game","eigenschap_name":"Strategie","eigenschap_id":24},{"cat_id":6,"cat_name":"Game","eigenschap_name":"Avontuur","eigenschap_id":25},{"cat_id":6,"cat_name":"Game","eigenschap_name":"Simulatie","eigenschap_id":26}]

HTML View:

<select data-ng-options="i.eigenschap_id as i.eigenschap_name for i in eigenschappen.game" ng-model="chosen"></select>

Generated HTML:

<select ng-model="chosen" data-ng-options="i.eigenschap_id as i.eigenschap_name for i in eigenschappen.game" class="ng-pristine ng-valid">
    <option value="?" selected="selected"></option>
    <option value="0">RPG</option>
    <option value="1">Strategie</option>
    <option value="2">Avontuur</option>
    <option value="3">Simulatie</option>
</select>

Answer №1

When Angular populates the select options in the HTML, the value attribute for each option may not align with what you expect. It is assigned as follows: - When an array is used as the data source, the value is set to the index of the array. - When an object is used as the data source, the value is set to the key or property name.

Upon selecting an option, Angular matches the index or key to retrieve the corresponding value from the array or object. This retrieved value becomes the model that is stored, rather than the visible value in the HTML, leading to potential confusion.

For instance, with an array data source, Angular will update the model with the value located at the index specified by the selected option value. If the array consists of [2013, 2014, 2015], the generated HTML values for the options will be 0, 1, and 2 respectively. Selecting the second item would result in Angular fetching the value 2014 from array[1] to update the model accordingly.

Reference: Insight shared by Mark RajCok on http://docs.angularjs.org/api/ng.directive: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

Encountering an issue with WebDriver in the realm of JavaScript

I am struggling to use JavaScript to locate specific controls and send values to them. One example is changing the text in a textbox with the ID "ID" to "123456". Below is the code I tried: ((IJavaScriptExecutor)driver).ExecuteScript("document.getElement ...

Unable to retrieve this information using $http callback

I am currently working with angular 1.5 and typescript, but I am facing an issue where I cannot access the 'this' property from the callback returned by the $http promise. Whenever I try to access a private method from the callback, 'this&a ...

Guide to integrating react-phone-number-input into material-ui TextField

Would it be possible for me to use a Material UI TextField component as the inputComponent prop for the PhoneInput component from react-phone-number-input? I am facing an issue where I am unable to apply the ref successfully. Even though I can see the Mat ...

Unexpected Results from WordPress Ajax Request

Currently, I am utilizing the snippets plugin in conjunction with Elementor. To implement an ajax function as a snippet, I have set it up like so: add_action( 'wp_ajax_get_slug_from_id', 'get_slug_from_id' ); add_action( 'wp_ajax_n ...

Using Javascript, print the port number to the console

I am currently developing a small Electron app with node.js and I am facing an issue with outputting the port my application is connected to for development purposes. Below is my MySQL connection code snippet: const mysql = require('mysql'); c ...

Every time I attempt to execute mupx deploy, an error message appears

issue in console shubhabrata@shubhabrata-VirtualBox:~/Meteor/myapp$ mupx deploy Meteor Up: Advancing Meteor Deployments for Production Configuration file : mup.json Settings file : settings.json “ Discover Kadira! A powerful tool to monitor yo ...

The Angular JS routes are not properly loading the required file from the provided TemplateURL when receiving a response from Node

I am trying to retrieve data from a MySQL database using node (routes.js) and have the results injected into club.html, which is then dynamically updated in index.html using ng-view. However, it seems that the JSON response from node is displaying directly ...

Tips for preventing users from entering special characters into input fields

How can I prevent users from entering the # character into an HTML input field? I attempted to use the pattern attribute, but it does not seem to be effective. I included this pattern in my input element: pattern="[^-#]+" I expected that this would disa ...

In what location can event listeners be found in npm packages?

For a while now, I've been immersed in the world of coding as I work on creating my very own IRC bot using nodejs. This project serves as an invaluable learning experience for me, and as I navigate through the process, I constantly encounter event lis ...

Every time I select a value for the first time, the ng-repeat array updates table data, but it doesn't update the table data just once

I am facing an issue with updating the table view based on select option in my project. The problem is that the table view updates only once when I select the option for the first time, but it doesn't update when I choose the option again. I'm ha ...

Issues arise when attempting to delete messages that have already been retrieved

Having trouble removing messages from a specific user without any success: bot.js client.on("message", (message) => { if (message.content === '$deleteuser') { message.channel.fetchMessages({limit: 10}).then(collec ...

When opting for "Not now" in Firefox, the error callback in getUserMedia is not activated

I am currently working on a script to detect when the user either allows or denies the use of a microphone using the getUserMedia API. UPDATE: To better illustrate the issue I am facing, I have created a fiddle: http://jsfiddle.net/4rgRY/ navigator.getUs ...

Getting field values from Firestore in a React application

Within my firestore document, I have three fields that store boolean values critical for subsequent processing. To access these boolean values in my program, I need to figure out how to read the fields of a document. This process should be straightforward, ...

Ways to avoid extra function loads in JavaScript

I'm currently working on an instant search feature and have some code implemented: $(function() { var timer; $("#searchterm").on('keypress',function() { timer && clearTimeout(timer); timer = setTimeout(doStuff, 800); tim ...

The Evolution of Bulma's Navigation Menu

Creating a transparent menu in Bulma has been successful for the desktop viewport: VIEW DESKTOP MENU However, when attempting to implement the same design on mobile, the menu ends up like this: VIEW MOBILE/TABLET MENU The mobile version seems to inheri ...

The scrolling speed of Ionic Load More feature is a bit sluggish

Hey there! I'm a newcomer to Ionic and AngularJS. In my current project with Ionic v1, the load more scrolling feature is extremely sluggish. I've attempted two different methods: Using a Service Using a Factory Both approaches are proving t ...

Tips on transforming a JSON file into a response for my personal server

After successfully converting an Excel file to JSON format using my code, I encountered a challenge when trying to convert this as a response. Despite attempting to use res.send in the JS code, it only displayed directory/inner codes instead of a response. ...

Creating messages from an array using vanilla JavaScript or jQuery

I have an array of objects containing messages that I want to display on my webpage, but I'm unsure how to approach it. [{"message":"dwd","user":"csac","date":"07.04.2021 20:46"}, {"mes ...

A solution for accessing computed properties within a v-for loop

Currently, I am facing a challenge with the code provided below. It seems that computed properties do not support parameters. Do you happen to have any suggestions on how to overcome this issue? I am considering using watchers on functions but I am also ...

Long-term responsibilities in Node.js

Currently, I have a node.js server that is communicating between a net socket and a python socket. The flow is such that when a user sends an asynchronous ajax request with data, the node server forwards it to Python, receives the processed data back, and ...