以下すべてのカテゴリを一覧表示するJSONを解析するAngularJSコードがあります。カテゴリボタンをクリックすると、カテゴリに関連付けられた各製品を表示したいので、カテゴリボタンをクリックすると、そのカテゴリ内のすべての製品のリストが表示されます。この問題についてはAngularJSで選択したカテゴリの製品を表示
(function() {
var app = angular.module('store', []);
app.controller('StoreController', ['$scope', function($scope) {
var vm = this;
$scope.products = [{
"category": "Cat1",
"name": "Product1"
}, {
"category": "Cat1",
"name": "Product2"
}, {
"category": "Cat2",
"name": "Product3"
}, {
"category": "Cat3",
"name": "Product4"
}]
$scope.categories = Object.keys($scope.products.reduce(function(categoryMap, product) {
categoryMap[product.category] = 1;
return categoryMap;
}, {}));
vm.selectCategory = function(category) {
vm.selectedCategory = category;
}
}]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html lang="en" ng-app="store">
<body ng-controller="StoreController as vm">
<div ng-repeat="category in categories" class="category">
<button ng-click="vm.selectCategory(category);">{{category}}</button>
</div>
<!-- RESULTS -->
<div ng-repeat="product in vm.selectedCategory" class="product">
<p>{{product.name}}</p>
</div>
</body>
</html>