ng-options过滤条件

时间:2020-05-01 16:12:52

标签: angularjs ng-options

我有一个带有ng-options + filter的选择,并且需要将条件应用于过滤器本身。因此,如果满足特定条件,则应用过滤器,否则不应用。预先感谢。

# example
if (elb_vm.elbInstances[index]['new_record?']){
   apply ng-options filter.
 }
<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>

1 个答案:

答案 0 :(得分:1)

您可以使用两个select元素,一个带有过滤器,另一个不带有过滤器,并根据ng-if显示它们,例如:

ng-if="elb_vm.elbInstances[index]['new_record?']"

演示:

var app = angular.module('myApp', []);
app.controller('AppCtrl', function($scope) {
  $scope.selected = "";
  $scope.condition = false;
  $scope.types = ["publicExtra","public","private"];
  
  $scope.toggleCondition = function(){
    $scope.condition = !$scope.condition;
  }
});
<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">
    <button ng-click="toggleCondition()">Toggle Condition</button> {{ condition }}<br><br>
    <select ng-model="selected" ng-if="condition"
      ng-options="('Subnet: ' + type) for type in types | filter: '!public' : true">
    </select>
    <select ng-model="selected" ng-if="!condition"
      ng-options="('Subnet: ' + type) for type in types">
    </select>
  </div>
</section>