Raghavan Ravi
Raghavan Ravi

Reputation: 59

angularjs ng-options nested json data

I am using AngularJS for my web app. My objective is to have two dropdowns list using ng-options.

Being still new to AngularJS, I am able to display the data but the entire object is displayed as a single option, I am unable to split each data.

Kindly find the code below that I 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: Dutch, English and French should be each separate dropdown option when Belgium is selected. & English and French should be each separate dropdown option when France is selected.

Kindly review the code and let me know if i had missed anything. Your help is deeply appreciated.

Upvotes: 2

Views: 1187

Answers (1)

Mistalis
Mistalis

Reputation: 18279

I fixed some issues in your code.

JSFiddle demo

The first input was correct:

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

But in the second, I changed several things:

  • Set ng-model to a different variable (selectLang), it's cleaner.
  • ng-options loops over selectedRegion.regionSelector, instead of just selectedRegion. That was your main mistake here:

<select ng-model="selectLang" ng-options="lang for lang in selectedRegion.selectLang"></select>

Upvotes: 2

Related Questions