Angular Js - Example - List Item using ng-repeat
Angular Js - Example
Directive: ng-repeat
Repeat the json data
HTML
<div ng-app ng-controller="MyCtrl">
<h1 ng-repeat-start="mainCategory in mainCategories | orderBy">{{mainCategory}}</h1>
<!-- main categories ngRepeat starts here -->
<ul ng-repeat-end>
<!-- main categories ngRepeat ends here -->
<li ng-repeat="category in categories | orderBy:'name' | filter:{ 'main_category': mainCategory}">{{category.name}}</li>
<!-- categories are filtered by mainCategory -->
</ul>
</div>
JavaScript
function MyCtrl($scope) {
var categories = [{
name: 'somethingA2',
main_category: 'A'
}, {
name: 'somethingB1',
main_category: 'B'
}, {
name: 'somethingB2',
main_category: 'B'
}, {
name: 'somethingA3',
main_category: 'A'
}, {
name: 'somethingB3',
main_category: 'B'
}, {
name: 'somethingA1',
main_category: 'A'
}];
$scope.categories = categories;
/** main categories - reduce the categories array to a dictionary object, and get the keys of the dictionary to get the unique main category names **/
$scope.mainCategories = Object.keys(categories.reduce(function (main, category) {
main[category['main_category']] = '';
return main;
}, {}));
}