monstro monstro - 3 years ago 167
AngularJS Question

AngularJS ng-repeat to display group names from JSON data

How Display stores,bookmarks,favorites using below JSON Data

JSON data (vm.bookmarks):

[
{
"category": "stores",
"title": "store1",
"link": "http://www.store1.com",
},
{
"category": "stores",
"title": "store1",
"link": "http://www.store2.com",
},
{
"category": "bookmarks",
"title": "bookmark1",
"link": "http://www.bookmark1.com",
},
{
"category": "bookmarks",
"title": "bookmark2",
"link": "http://www.bookmark2.com",
},
{
"category": "bookmarks",
"title": "bookmark3",
"link": "http://www.bookmark3.com",
},
{
"category": "favorites",
"title": "favorites1",
"link": "http://www.favorites1.com",
},
{
"category": "favorites",
"title": "favorites2",
"link": "http://www.favorites2.com",
}
]


I want to list only categories' names using ng-repeat:

<div ng-repeat="item in vm.bookmarks|groupBy:'category'">
<h2>{{item.category}}</h2>
</div>


I want this to be displayed:


  • stores

  • bookmarks

  • favorites



But it displays nothing! What am I doing wrong?

P.S

I was using filters without injecting anything, like this:

<div ng-repeat="item in vm.bookmarks|filter:{category:'flyers'}" >
<a href="{{item.link}}">{{item.title}}</a>
</div>


Not sure if I need to inject angular-filter to use groupBy filter - why?

Answer Source

You do not need to use (key, value) , also you need to inject angular.filter as dependency

var MyApp = angular.module("MyApp",['angular.filter']);

<ul class="nav nav-tabs" data-tabs="tabs" 
    ng-repeat="bookmark in vm.bookmarks|groupBy:'category'">
    <li><a href="@tabid" data-toggle="tab">{{bookmark.category}}</a></li>
</ul>

DEMO

var app = angular.module("myApp", ['angular.filter']);
app.controller("SimpleController", function($scope) {
  this.bookmarks = [
    {
        "category": "stores",
        "title": "store1",
        "link": "http://www.store1.com",
    },
    {
        "category": "stores",
        "title": "store1",
        "link": "http://www.store2.com",
    },
    {
        "category": "bookmarks",
        "title": "bookmark1",
        "link": "http://www.bookmark1.com",
    },
    {
        "category": "bookmarks",
        "title": "bookmark2",
        "link": "http://www.bookmark2.com",
    },
    {
        "category": "bookmarks",
        "title": "bookmark3",
        "link": "http://www.bookmark3.com",
    },
    {
        "category": "favorites",
        "title": "favorites1",
        "link": "http://www.favorites1.com",
    },
    {
        "category": "favorites",
        "title": "favorites2",
        "link": "http://www.favorites2.com",
    }
];
var vm = this;
  
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.16/angular-filter.js" > </script>
<body ng-app="myApp">
    <div>
      <div data-ng-controller="SimpleController as vm">
      <ul class="nav nav-tabs" data-tabs="tabs" 
    ng-repeat="(key, value) in (vm.bookmarks|groupBy:'category')">
    <div ng-repeat="cat in value | unique:'category' ">
          <h1> {{cat.category}}</h1>
    </div>
  </ul>
        </div>
    </div>
</body>

</html>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download