0

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>

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.