ng-options values are set as objects rather than strings

I am in the process of developing a small Angular (1.5) application that includes two linked drop-down menus. The options in the second drop-down menu are dynamically updated based on the selection made in the first drop-down menu. Everything functions as expected, but I have encountered a minor issue. In the HTML file, the value attribute within the option tag is assigned to object:[some number] instead of displaying the actual option value. The displayed value between the <option> and </option> tags appears correctly. Please refer to the screenshot below for clarification. How can I ensure that the value attribute (value="") matches the value displayed between the opening and closing tags?

https://i.sstatic.net/1Vgj7.png

HTML File:

<div ng-app="App" ng-controller="app-controller">
  <select id="brand"
    ng-model="input.selected_brand"
    ng-options="a for (a,b) in data"
  >
    <option value="" disabled selected>Selection 1</option>
  </select>

  <select id="model"
    ng-model="input.selected_model"
    ng-options="a.model for a in input.selected_brand"
  >
    <option value="" disabled selected>Selection 2</option>
  </select>
</div>

JS file:

var App = angular.module("App", [])
.controller('app-controller',function($scope,$http){
  // Fetch JSON data
  $http.get('data/data.json').then(function(res){
    $scope.data = res.data;
    $scope.selected_brand = $scope.data;
  });
});

JSON file:

{
  // JSON data content here
}

The purpose behind this setup is to enable

  $scope.submit = function(input){
    $scope.input_data = angular.copy(input);
    console.log($scope.input_data);
  }

This function requires accurate values to perform correctly. Currently, the output from console.log appears as follows:

Object { selected_brand: Array[10], selected_model: Object }

Answer №1

Here is a suggested solution:


    <select id="brand"
    ng-model="selected_brand"
    ng-options="a as a for (a,b) in options.data"
  >
    <option value="" disabled selected>Choose Brand</option>
  </select>

  <select id="model"
    ng-model="selected_model"
    ng-options="model.model as model.model for model in options.data[selected_brand]"
  >
    <option value="" disabled selected>Choose Model</option>
  </select>

It's important to note that the actual value passed to the model is what truly matters, not the internal references used by AngularJS. Feel free to customize this code snippet to meet your specific requirements.

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

Sorting character values in TypeScript using ascending and descending order in a JSON array

In my dataset of homes, I have the following information: var homes = [ { "h_id": "3","city": "Dallas","state": "YYYY","zip": "75201","price": "162500" }, { "h_id": "4","city": "CA","state": "ZZZZ","zip": "90210","price": "319250" }, { "h ...

Ways to extract code within delimiters

After loading a template from a file, the structure is as follows: [[style]].someclass{padding:10px 15px}[[/style]] [[code]]<tr><td class="someclass">{{text}}</td></tr>[[/code]] The template is loaded using .get() and stored in th ...

prompting users in a node.js application

I need help with querying the user multiple times in my Node.js application. Currently, all responses get printed out together and the first response is processed by both functions. I suspect this issue arises from the asynchronous nature of Node.js. Can s ...

Decoding a JSON object in ASP.NET containing both singular values and arrays

Seeking guidance on json as a beginner! Let's consider the below json object: { "Table": "", "Id": "", "Column": [ { "ColumnText": "", "ColumnSqlName": "" }, { "ColumnText": "", ...

Dealing with Angular's *ngFor and Filtering Data

Can anyone help me with the best way to display my JSON data in an Angular 7 template? In the JSON example below, I have a person object with multiple identification numbers. I'm currently using *ngFor to iterate through them in the template and *ngIf ...

Exploring the wonders of HTTP requests through pure pipes in Angular 5

I've been working on a pipe that converts currency values from one to another by making an HTTP call. The idea is to pass in the source and destination currencies as parameters. @Pipe({ name: "currencyConverter" }) export class CurrencyConverterP ...

Angulating Service Testing

I am encountering an issue that I'm not sure how to resolve because I am inexperienced when it comes to testing. Currently, I am testing a service that includes the following code: import { Injectable } from '@angular/core'; import { Endpo ...

jQuery's $.ajax method no longer supports XHR

My current code looks like this: $.ajax(options) .then(function (response) { // success }, function (response, a, b) { // fail }) .always(function (output, status, xhr) { // xhr is always null here }); In the p ...

Implementing Codeigniter AngularJs routing with an initial base URL

I'm facing a challenge with routing in AngularJS to align with the base URL set in the config of CodeIgniter. Base URL = http://localhost:8080/example/ For instance, if my current URL is http://localhost:8080/example/details and there's an ...

Mesh height determines the color scheme in Three.js

After loading an STL file using the STLLoader() in Three.js and converting it into a mesh, I am interested in coloring each cube of the mesh based on its height. The goal is to achieve a visual effect similar to the one shown in this image screenshot-color ...

Troubleshooting the Issue with Counting JSON Nodes in AJAXейства

So, I have this JSON snippet: { "productDetails": { "productThumb": "/uploads/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="215544525560454c484f614548594e4f424e4c0f424e4c">[email protected]</a>/showup-bg4.jp ...

Removing all repetitions from an array in JavaScript

My collection of objects includes the following inputs: var jsonArray1 = [{id:'1',name:'John'},{id:'2',name:'Smith'},{id:'3',name:'Adam'},{id:'1',name:'John'}] There is a dupl ...

Output a message to the Java console once my Selenium-created Javascript callback is triggered

My journey with Javascript has led me to mastering callback functions and grasping the concept of 'functional programming'. However, as a newcomer to the language, I struggle to test my syntax within my IntelliJ IDE. Specifically, I am working on ...

Every time I switch views using the router in vue.js, my three.js canvas gets replicated

After creating a Vue.js integrated with three.js application, I encountered an issue with the canvas getting duplicated every time I opened the view containing the three.js application. The canvas remained visible below the new view, as shown in this image ...

You are unable to access the array beyond the scope of the function

I am encountering an issue with a function I wrote: function gotData(data){ result = data.val() const urls = Object.keys(result) .filter(key => result[key].last_res > 5) .map(key => ({url: 's/price/ ...

Converting innerJsonObject to JSON using GSON library

My task involves creating a specific JSON Object and converting it to a string using the GSON library (toJson(Object)). The issue I'm facing is that GSON adds the nameValuePair with each JSON Object. How can I resolve this? "Param_1": { "SubParam_1" ...

Angular and RxJS work together to repeat actions even when an HTTP request is only partially successful

I am currently attempting to achieve the following: Initiate a stop request using a stored ID in the URL. Send a request with a new ID in the URL and save this new ID. If the response code is 200, proceed as normal. If it is 204, repeat steps 1 and 2 with ...

When images in Ionic overlap with the side menu bar

Currently, I am in the process of developing an Android app using the IONIC framework. Within this project, I have created three divisions that contain images with shadows applied to them. However, I am encountering an issue where the side menu bar is over ...

Accessing JSON data from outside the src folder in React.js

I am facing an issue with my React project as I try to import data from a JSON file located outside of the project folder. Here is the code snippet I am using: const env = require('./../../env.json'); However, this results in the following err ...

Error while parsing Json - Start byte 0xa0 is not valid UTF-8 notation

I am facing a problem with sending Json data to the server. It seems like there are some bad characters at the beginning of the UTF-8 format that are causing issues. To fix this, I tried using CharDecoder to replace all the malformed utf-8 characters. Her ...