带条件的 ng-options 过滤器
ng-options filter with condition
我有一个带有 ng-options + 过滤器的 select,需要对过滤器本身应用条件。因此,如果满足特定条件,则应用过滤器,否则不应用。提前致谢。
# 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>
您可以使用两个 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>
我有一个带有 ng-options + 过滤器的 select,需要对过滤器本身应用条件。因此,如果满足特定条件,则应用过滤器,否则不应用。提前致谢。
# 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>
您可以使用两个 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>