Фильтр ng-options с условием - PullRequest
0 голосов
/ 01 мая 2020

У меня есть выбор с помощью ng-options + filter, и мне нужно применить условие к самому фильтру. Таким образом, если определенное c условие выполнено, тогда применять фильтр иначе не будет. Заранее спасибо.

# 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 Ответ

1 голос
/ 01 мая 2020

Вы можете использовать два 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>
...