1

I have a list of regions, and each region has a drop-down list that should show only the cities in that particular region. I used a nested ng-repeat for this:

<li ng-click="filter_region($event)" ng-repeat="region in regions">
       <span>{{region.region}}</span>
    <ul class="cityFilter" >
            <li ng-repeat="city in cities | filter:cityList">
                <input type="checkbox" value="{{city.id}}" ng-model="city.checked" ng-checked="city.checked">
                <span>{{city.city}}</span>
            </li>
        </ul>
  </li>

I need cityList as a dynamic filter. Each city should be filtered based on the current region. i.e. the region value needs to change with each iteration of the outer loop

pseudo-code for the filter:

$scope.cityList = function (item) {
            if (item.region_id == myDynamicRegion) 
                return true;
            return false;
        }

Is it doable?

1
  • please put code on jsfiddle/plunker Commented Oct 8, 2013 at 11:00

1 Answer 1

1

Yes, it is doable. You can pass arguments to your filter:

<div ng-repeat="item in items |filter:x"></div>

And the filter:

 yourApp.filter('filter', function () {
        return function (item, x) {
            // Do the magic.
        };
 });
Sign up to request clarification or add additional context in comments.

2 Comments

Where do I manipulate the value to match the current region? I need the value to change for each iteration
Can't you pass the region to the filter and show/hide the city based on that region?

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.