Exploring JSON data hierarchies with AngularJS using ng-options

In my web app, I am utilizing AngularJS to create two dropdown lists using ng-options.

  • The first dropdown displays a list of countries
  • The second dropdown provides language preferences for the selected country

As I am still new to AngularJS, I am able to display the data but it is being shown as a single option instead of separate options.

Below is the code snippet that I have used:

HTML

<select ng-model="selectedRegion" ng-options="region.countryName for region in eyebrow.regionSelector"></select>
<select ng-model="selectedRegion.selectLang" ng-options= "selectedRegion.selectLang for region in selectedRegion track by selectedRegion.countryName"></select>

JS:

angular.module ('appngOption')
.controller ('headerController', function($scope) {
  $scope.eyebrow = { regionSelector: [
  {
     "id": 1,
     "countryName": "Belgium",
     "selectLang": ["Dutch", "English", "French"]
  },{
     "id": 2,
     "countryName": "France",
     "selectLang": ["English", "French"]
  }]}
});

Example: When selecting Belgium, there should be individual dropdown options for Dutch, English, and French. For France, there should be separate dropdown options for English and French.

Please review the code and let me know if I have missed anything. Your assistance is greatly valued.

Answer №1

I made corrections to your code.

Check out the JSFiddle demo

The first input was fine:

<select ng-model="selectedRegion" ng-options="region.countryName for region in eyebrow.regionSelector"></select>

However, in the second input, I made some changes:

  • I updated ng-model to a cleaner variable name (selectLang).
  • The ng-options now loops over selectedRegion.selectLang instead of just selectedRegion. This was the main mistake in your code:

<select ng-model="selectLang" ng-options="lang for lang in selectedRegion.selectLang"></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

How can the count of specific values matching total records be determined in Angular/TypeScript using JSON?

In the dataset provided below, how can we determine the count of applicable impacts, non-applicable impacts, and FYI impacts for nested records under the assigned_to key instead of the parent record? The expected results should be: For 1st Record Appl ...

Unable to retrieve information from the secure https web interface

Encountered an error while running the main thread: Error message: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to fin ...

"Can you provide guidance on binding data in vue.js when there is a dash (-) in the key of the JSON

My JSON data is structured as follows: { "color-1": "#8888", "color-2": "#000" } I am attempting to bind this variable with a style tag for a Vue component. However, my current approach seems to not be functioning as expected. <div v-bind:sty ...

In angular.js, repeating elements must be unique and duplicates are not permitted

My view controller includes this code snippet for fetching data from an API server: $scope.recent_news_posts = localStorageService.get('recent_news_posts') || []; $http({method: 'GET', url: 'http://myapi.com/posts'} ...

The container layout in the Angular Material design page is not displaying correctly

After developing a basic page in AMD on my local machine, I decided to use <div layout="row"> to split the content into two columns similar to the bootstrap col-md-6 setup. I found this useful link: [https://material.angularjs.org/1.0.5/layout/contai ...

Discover the size of objects in three.js

Looking for a way to determine the dimensions of a mesh in three.js? I have Collada (.dae) files and need to know their size in units (x,y,z). I've heard about using geometry.computeBoundingBox(), but could use more guidance on how to implement it. ...

Leveraging JSON Data for Avatars in React.js

Struggling to arrange 10 Avatars side by side for showcasing the user list. However, encountering an issue where the Avatars are being duplicated and showing incorrect initials when passing JSON data. Experimented with this in Code Sandbox linked below. h ...

Having trouble selecting all checkboxes in the tree using angular2-tree when it first initializes

My goal is to have all checkboxes auto-checked when clicking the "feed data" button, along with loading the data tree. I've attempted using the following code snippet: this.treeComp.treeModel.doForAll((node: TreeNode) => node.setIsSelected(true)); ...

What is the proper way to activate speech recognition on electron?

I have a chatbot application running on Electron, and I am in need of implementing speech-to-text functionality. I initially tried using window.SpeechRecognition and window.webkitSpeechRecognition, but it seems like Chrome does not support speech recogniti ...

Unable to utilize jQuery's .append(data) function due to the need to use .val(append(data)) instead

I have been attempting to utilize JQuery .append(data) on success in order to change the value of an input to the appended data like this: .val(append(data)), but it doesn't seem to be working. Surprisingly, I can successfully change the value to a st ...

The Angular JS demo is failing to run. The error message "Argument 'UserCtrl as uCtrl' is not a function, got undefined" is displayed

After watching a tutorial on http://www.youtube.com/watch?v=HCR7i5F5L8c#t=587, I decided to try out the example myself. I added AngularJS 1.0.8 but encountered an error: Argument 'UserCtrl as uCtrl' is not a function, got undefined I suspect ...

Issues with Angular HTML failing to load correctly

Greetings, I am seeking assistance with an issue that has been challenging me for quite some time. As a newcomer to Angular JS, I may be overlooking something obvious. Here is the scenario: I am utilizing ng-repeat on a directive in the following manner: ...

Can routes be nested in React components?

I'm curious to know if there's a way to integrate nested routes in React, where the Navbar component serves as the parent for dashboard and properties. <Router> <Routes> <Route path='/' element={<Home />} /> ...

Using jQuery to remove all inline styles except for a specific one

Just a quick inquiry: Our Content Management System (CMS) utilizes CKEditor for clients to modify their websites. The front-end styles include the use of a pre tag, which we have customized to our desired appearance. However, when their team members copy a ...

Clicking on the button will result in the addition of new

Having some trouble with jQuery as I try to dynamically add and remove HTML elements (on click of +) while also making sure each element has a unique name and ID like "name_1", "name_2"... Unfortunately, things aren't quite going as planned. Below i ...

I'm encountering problems when attempting to display the image/png response from an xmlHTTPRequest. Instead of the correct data, I

I have implemented the following code to integrate a captcha generating web service. The response data is successfully obtained, but when I attempt to display the result within a div element, the image appears as distorted text. var xmlHttp = new XMLHtt ...

Tips on converting a multi-indexed dataframe, a dataframe that is grouped by multiple columns, into a nested JSON format

I have a Pandas Series that I obtained by applying a groupby operation on a DataFrame with columns 'var' and 'month', then summing up the corresponding data. The result, with 'var' and 'month' as indexes, looks like ...

Understanding how to set the Content-Type in a Post request using Ajax with Node.js

Attempting to send an ajax request without a header in Node.js triggers an error: { [Error: unsupported content-type] status: 415, statusCode: 415 } However, when sending a request with a header, Node.js does not seem to respond. Here is my ajax funct ...

The lower text box on the page being covered by the virtual keyboard on IOS

Our website features a fixed header and footer with scrollable content. We have 20 text boxes on the page, but the ones at the bottom, like Zip and Telephone, are obscured by the iOS virtual keyboard that appears when a text box is clicked. If we could d ...

What might be causing the issue in the build process of my next.js project?

**Why is my Node.js YAML file causing an error?** name: Node.js CI on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: build: runs-on: ubuntu-latest strategy: matrix: node-ver ...