Angularjs select 如果不在数组中则显示值
Angularjs select display value if not in array
我想创建一个 select 用于在我的数组中添加和删除值。但该值必须是唯一的。
这是我的代码,但无法正常工作,因为我不知道如何隐藏或显示 select
中的值
<div ng-app="myApp">
<div ng-controller="myController">
<p>Select something</p>
<select ng-model="selectedBucket"
ng-options="row as row.display for row in bucketEnum | filter:bootstrapValues">
</select>
<button ng-click="add(selectedBucket.value)">
add
</button>
<br>
{{initialSet}}
<br>
{{bucketEnum}}
</div>
</div>
以及控制器和过滤器:
var app = angular.module('myApp', []);
app.controller('myController', function ($scope, $filter) {
$scope.initialSet = [1, 2, 4, 7];
$scope.bucketEnum = [
{ display: '0', value: 0, bool:true },
{ display: '1', value: 1, bool:true},
{ display: '2', value: 2, bool:true },
{ display: '3', value: 3, bool:true },
{ display: '4', value: 4, bool:true },
{ display: '5', value: 5, bool:true },
{ display: '6', value: 6, bool:true },
{ display: '7', value: 7, bool:true }
];
$scope.add = function(value){
$scope.initialSet.push(value);
$scope.selectedBucket = $filter('bootstrapValues')($scope.initialSet, $scope.bucketEnum);
}
});
app.filter('bootstrapValues', function(){
return function(initial, baseBucket){
var result = [];
for(var i = 0; i < initial.length; i++){
var flag = false;
for(var j=1; j<baseBucket.length; j++){ //from 1 or 0.. you call
if(initial[i] === baseBucket[j].value){
flag = true;
baseBucket[j].bool = false;
break; // if there are repeated elements
}
}
}
return result;
};
});
我想创建一个 select 用于在我的数组中添加和删除值。但该值必须是唯一的。
这是我的代码,但无法正常工作,因为我不知道如何隐藏或显示 select
中的值 <div ng-app="myApp">
<div ng-controller="myController">
<p>Select something</p>
<select ng-model="selectedBucket"
ng-options="row as row.display for row in bucketEnum | filter:bootstrapValues">
</select>
<button ng-click="add(selectedBucket.value)">
add
</button>
<br>
{{initialSet}}
<br>
{{bucketEnum}}
</div>
</div>
以及控制器和过滤器:
var app = angular.module('myApp', []);
app.controller('myController', function ($scope, $filter) {
$scope.initialSet = [1, 2, 4, 7];
$scope.bucketEnum = [
{ display: '0', value: 0, bool:true },
{ display: '1', value: 1, bool:true},
{ display: '2', value: 2, bool:true },
{ display: '3', value: 3, bool:true },
{ display: '4', value: 4, bool:true },
{ display: '5', value: 5, bool:true },
{ display: '6', value: 6, bool:true },
{ display: '7', value: 7, bool:true }
];
$scope.add = function(value){
$scope.initialSet.push(value);
$scope.selectedBucket = $filter('bootstrapValues')($scope.initialSet, $scope.bucketEnum);
}
});
app.filter('bootstrapValues', function(){
return function(initial, baseBucket){
var result = [];
for(var i = 0; i < initial.length; i++){
var flag = false;
for(var j=1; j<baseBucket.length; j++){ //from 1 or 0.. you call
if(initial[i] === baseBucket[j].value){
flag = true;
baseBucket[j].bool = false;
break; // if there are repeated elements
}
}
}
return result;
};
});