在 ng-options 中排除值

exclude value with in ng-options

我一直在尝试弄清楚如何从 ng-options 使用过滤器生成的数组中排除一个值,但没有成功。

下面代码片段生成的数组生成以下数组["publicExtra","public","private"]我想从选项中排除"public"。提前致谢。

<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">

您可以使用 filter 并在搜索字符串之前使用 ! 轻松地执行此操作以否定谓词,例如:

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

但请注意,这会忽略 publicpublicExtra,因为基本过滤器不会进行精确匹配。为此,我们还需要为 comparator 传递 true,例如:

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>