angularjs ng-options nested json data

寵の児 提交于 2019-12-12 10:11:06

问题


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

  • The first dropdown showing the country list
  • The other gives the language preferences for the selected country

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.


回答1:


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>


来源:https://stackoverflow.com/questions/41935602/angularjs-ng-options-nested-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!