带条件的ng-选项过滤器

问题描述 投票:0回答:1

我有一个带有ng-options + filter的select,需要对过滤器本身应用一个条件。因此,如果满足特定的条件,则应用过滤器,否则不要。先谢谢你了,我有一个带有ng-options+filter的select,需要对filter本身应用一个条件,如果满足特定的条件就应用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>
angularjs ng-options
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.