gpt4 book ai didi

javascript - AngularJS 按类别过滤

转载 作者:行者123 更新时间:2023-12-03 06:37:16 25 4
gpt4 key购买 nike

我正在尝试按类别显示菜单。每个类别的名称是菜单项数组的键的名称,例如“布朗尼蛋糕”、“蛋糕”。

这就是我所引用的内容,但似乎有些不对劲:

filter list of items when clicking category link

html:

<section class="choices">
<div class="categories">
<ul>
<li ng-repeat="menu in fullMenu">
<a ng-repeat="(key,val) in menu" href="" ng-click="categoryFilters.category = {{key}}">{{key}}
</a>
</li>
</ul>
</div>

</section>

<section class="category" ng-repeat="menu in fullMenu | filter: categoryFilters">

<div ng-repeat="items in menu">
<ul>
<li ng-repeat="item in items">
<img src="{{item.image_url}}">
<h3>{{item.name}}</h3>
<p>{{item.description}}</p>
<p><span>$</span>{{item.price}}</p>
</li>
</ul>
</div>

</section>

JS:

  angular.module('bakeryMenuApp')
.controller('mainCtrl', function($scope, dataService) {
dataService.getMenus(function(response) {
$scope.fullMenu = response.data.menus;
$scope.categoryFilters = {}
});
})

JSON:

{  
"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":false,
"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":false
}
]
}
]
}

最佳答案

通过适当的方式检查条件即可实现

Controller && HTML

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.category = '';
$scope.categoryList = function(value) {
$scope.category = value;
}
$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": false,
"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": false
}]
}]
});
form.ng-pristine {
background-color: lightblue;
}

form.ng-dirty {
background-color: pink;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
<section class="choices">
<div class="categories">
<ul>
<li ng-repeat="menu in menus">
<a ng-repeat="(key,val) in menu" href="" ng-click="categoryList(key)">{{key}}
</a>
</li>
</ul>
</div>

</section>
<section class="category" ng-repeat="menu in menus">
<div ng-repeat="(key, items) in menu">
<ul>
<li ng-repeat="item in items" ng-show="category == key">
<div>
<img src="{{item.image_url}}">
<h3>{{item.name}}</h3>
<p>{{item.description}}</p>
<p><span>$</span>{{item.price}}</p>
</div>
</li>
</ul>
</div>

关于javascript - AngularJS 按类别过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38136788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com