[英]How to filter results based on checkbox selection in angular
我有3個復選框,我想根據選擇的checbox來過濾我的結果列表。 我正在形成一個選擇數組並使用angular應用過濾器,但是當我單擊復選框時,結果或列表將消失
HTML
<div class="row" ng-controller="DataCtrl">
<div class="col-sm-4">
<div class="checkbox">
<label>
<input type="checkbox" value="US" ng-click="select('US')">
US
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="UK" ng-click="select('UK')">
UK
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="Australia" ng-click="select('Australia')">
Australia
</label>
</div>
</div>
<div class="col-sm-8">
<div class="well" ng-repeat="eve in events | filter : selected_country">
<h3>{{eve.title}}</h3>
<h5>{{eve.country}}</h5>
</div>
</div>
</div>
調節器
refine.controller('DataCtrl', function($scope){
$scope.selected_country = [];
$scope.events = [
{
'title' : 'Global Warming 2016',
'country' : 'US'
},
{
'title' : 'Global Warming 2017',
'country' : 'UK'
},
{
'title' : 'Global Warming 2018',
'country' : 'Australia'
}
];
$scope.select = function(country){
var index = $scope.selected_country.indexOf(country);
var country_exists = (index > -1);
if(country_exists == false){$scope.selected_country.push(country);}
else{ $scope.selected_country.splice(index, 1);}
console.log($scope.selected_country);
}
});
看來你寫錯了你的條件,試着改變
if(country_exists == false){$scope.selected_country.push(country);}
至
if(country_exists == true){$scope.selected_country.push(country);}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.