问题
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 overselectedRegion.regionSelector
, instead of justselectedRegion
. 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