user1576738
user1576738

Reputation: 121

ng-options with multiple conditions

I have a select that has a filter set to not show the 'public' string in an array (public, publicExtra and private) if a specific condition is met in the ng-if. But now I discovered that that is not the only condition required and it seems like I have to duplicate the select with different conditions in order to achieve what I want, this becomes very messy. Is there a way to create a filter in the controller and set the conditions there and then show the filter in the activate the filter on the select if all the conditions are met? Thanks in advance.

<select ng-model="elb_instance.use_vpc"
                    ng-if="elb_instance['new_record?']"
                    ng-options="('Subnet: ' + type) for type in elb_instance.vpc_names_with_subnet_types_for_elb[elb_instance.vpc_name] | filter: '!public' : true"
                    ng-show="elb_instance.vpc_name"
                    id="use_vpc"
                    class="input-medium">
                    <option value="">Select subnet</option>
            </select>```

Upvotes: 1

Views: 727

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Yes, you can use a custom filter with arg: | myfilter:conditions

<select ng-model="elb_instance.use_vpc"
        ng-if="elb_instance['new_record?']"
        ng-options="('Subnet: ' + type) for type in elb_instance.vpc_names_with_subnet_types_for_elb[elb_instance.vpc_name] | myfilter:conditions"
        ng-show="elb_instance.vpc_name">
    <option value="">Select subnet</option>
</select>

where myfilter can be something like:

app.filter('myfilter', function() {
   return function( items, conditions) {
    var filtered = [];

    angular.forEach(items, function(item) {
       if(conditions.aaa == true /* and other rules... */) {
          filtered.push(item);
        }
        else if(conditions.bbb == true){
          filtered.push(item);
        }        
    });

    return filtered;
  };
});

where items are elb_instance.vpc_names_with_subnet_types_for_elb[elb_instance.vpc_name]

Upvotes: 0

Related Questions