angularjsng-options

exclude value with in ng-options


I've been trying to figure out how to exclude a value from an array generated by ng-options using filters with no luck.

The array generated by the snippet below generates the following array ["publicExtra","public","private"] I want to exclude "public" from the options. Thanks in advance.

<select ng-model="elb_instance.use_vpc"
                    ng-options="('Subnet: ' + type) for type in elb_instance.vpc_names_with_subnet_types_for_elb[elb_instance.vpc_name]"
                    ng-disabled="!elb_instance['new_record?']"
                    ng-show="elb_instance.vpc_name"
                    id="use_vpc"
                    class="input-medium">

Solution

  • You can do this easily using filter and using ! before search string to negate the predicate like:

    ng-options="('Subnet: ' + type) for type in types | filter: '!public'">
    

    But note that this ignores both public & publicExtra as basic filter does not do exact match. For that, we will also need to pass true for the comparator like:

    ng-options="('Subnet: ' + type) for type in types | filter: '!public' : true">
    

    var app = angular.module('myApp', []);
    app.controller('AppCtrl', function($scope) {
      $scope.selected = "";
      $scope.types = ["publicExtra","public","private"];
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
    <section ng-app="myApp">
      <div ng-controller="AppCtrl">
        <select ng-model="selected" 
          ng-options="('Subnet: ' + type) for type in types | filter: '!public' : true">
        </select>
      </div>
    </section>