2016-04-04 81 views
0

我有這樣的對象:AngularJS集團通過對象屬性

$scope.items = [{ 
    "category": { "code": "10", "description": "Movies" }, 
     "item": {"code": "421", "description": "Noriko's Dinner Table" } 
        },{ 
    "category": { "code": "10", "description": "Movies" }, 
     "item": {"code": "511", "description": "Avatar" } 
        },{ 
    "category": { "code": "45", "description": "Books" }, 
     "item": {"code": "93", "description": "Divergent" } 
        },{ 
    "category": { "code": "45", "description": "Books" }, 
     "item": {"code": "72", "description": "Sense and Sensibility" } 
        },{ 
    "category": { "code": "33", "description": "TV Shows" }, 
     "item": {"code": "11", "description": "The Walking Dead" } 
        },{ 
    "category": { "code": "33", "description": "TV Shows" }, 
     "item": {"code": "60", "description": "The Bates Motel" } 
        }]; 

,我試圖將它們分組,這樣的列表可以整齊地組織和查看。就像這樣:

enter image description here

但是你可以看到,該類別的描述出現在每一個項目。我該怎麼辦?

<ul> 
    <li ng-repeat="item in items track by $id(item)"><div>{{item.category.description}}</div>{{item.item.description}}</li> 
    </ul> 

現場演示:http://plnkr.co/edit/mjek9xPo3ihBTiwW6U0V?p=preview

+0

我很困惑 - 蹲點看起來完全像你說的你試圖實現 – Maxwelll

+4

http://stackoverflow.com/questions/14800862/how-can-i-group-data-with-an-angular-filter – ajmajmajma

回答

2

你想要做這樣的事情:

<ul ng-repeat="(key, value) in items | groupBy: 'category.description'"> 
{{ key }} 
    <li ng-repeat="item in value"> 
    {{ item.item.description }} 
    </li> 
</ul> 

以建於groupByfilter angulars的優勢。