Abdou Tahiri
Abdou Tahiri

Reputation: 4408

Angularjs select using ng-options

i want to filter cities using states with 2 select inputes when the first select change it value the second one should be changed according to the first select.

i have an array of items :

$scope.items = [
     {
         name:'state1',
         cities : [
              {id:1,name:'city1'},
              {id:2,name:'city2'},
         ]
     },
     {
         name:'state2',
         cities : [
              {id:1,name:'city1'},
              {id:2,name:'city2'},
         ]
     },
];

i tried this but its not working : first for states :

<select ng-model="state" ng-options="s.id as s.name in states"  ></select>

second for cities :

<select ng-model="city" ng-options="s.cities.id as s.cities.name for s in items | filter:{s.name:state}" ></select>

Upvotes: 0

Views: 540

Answers (1)

Michel
Michel

Reputation: 28349

The first dropdown selects the whole country object. The options of the second dropdown are the cities of the selected country :

<select class="country" ng-model="selectedCountry" ng-options="country.name for country in items">
    <option value="">Choose country</option> 
</select>

<select ng-if="selectedCountry" class="city" ng-model="selectedCity" ng-options="city.name for city in selectedCountry.cities">
    <option value="">Choose city</option> 
</select>

Fiddle

Upvotes: 3

Related Questions