我想設置一個過濾系統,其中包含兩個過濾器「純素」和/或「無麩質」食物菜單。每個菜單項目對象都有「is_vegan」和「is_gluten free」,它們可以是真或假。AngularJS使用布爾值多鍵過濾
我試過用ng-models和angular的過濾函數使用複選框。
當前系統的問題是,即使我想搜索某些對於無麩質成分爲真的項目,它也會顯示「is_vegan:true」項目。
http://plnkr.co/edit/YZftSjR73ID6T1wndoFy?p=preview
HTML:
<body ng-app="bakeryMenuApp">
<div class="wrap" ng-controller="mainCtrl">
<div class="search-filters">
<div class="filter">
<input name="gluteen" type="checkbox" ng-model='search.type1' data-ng-true-value='true' data-ng-false-value='false'>
<label for="glueteen">Gluten Free</label>
</div>
<div class="filter">
<input name="vegan" type="checkbox" ng-model='search.type2' data-ng-true-value='true' data-ng-false-value='false'>
<label for="vegan">Vegan</label>
</div>
</div>
<section class="content-category" ng-repeat="menu in menus">
<div ng-repeat="(key, items) in menu" class="{{key}}">
<h2>{{key}}</h2>
<ul>
<li ng-repeat="item in items | filter:search.type1 | filter:search.type2">
<div class="img-container">
<img src="{{item.image_url}}" alt="{{item.name}} image">
</div>
<h3>{{item.name}}</h3>
<p>{{item.description}}</p>
<p class="content-filters">{{item.is_vegan}}, {{item.is_gluten_free}}</p>
<p class="price"><span>$</span>{{item.price}}</p>
</li>
</ul>
</div>
</section>
</div>
</body>
JS:
var app = angular.module("bakeryMenuApp", []);
app.controller('MainCtrl', function($scope) {
$scope.search=[];
$scope.menus = [{
"brownies": [{
"name": "Baker's Choice Bars Assortment",
"price": "45",
"description": "A beautiful and delicious assortment of Magnolia Bakery’s double fudge brownies, chocolate chunk blondies and magic cookie bars.",
"image_url": "https://pantograph0.goldbely.com/s364/uploads/product_image/image/8346/bakers-choice-bars-assortment.1ddd25a1f59a89a1de2d0583dab50000.jpg",
"is_vegan": true,
"is_gluten_free": false
}]
}, {
"cakes": [{
"name": "Raseberry Lemon Cake",
"price": "50",
"description": "Vanilla crème fraîche cake layered with raspberry Swiss meringue buttercream and lemon curd filling, covered with raspberry buttercream.",
"image_url": "http://www.empirecake.com/_main_site/wp-content/uploads/2014/12/Rasberry_Lemon_01_final_drkr-600.jpg",
"is_vegan": false,
"is_gluten_free": true
}]
}]
});
@ developer033因爲我認爲這就是我應該做的就是最底層的對象。這是錯的嗎? –
請'$ scope.search = [];'爲'$ scope.search = {};'或'$ scope.search = {'type1':null,'type2':null};' –
@JaeeunLee can你爲此做了一個小提琴? –