問題:我有一個屬於不同球隊的球員列表。如何使用 Angular 過濾器按球隊對球員進行分組和顯示?
範例資料:
[{name: 'Gene', team: 'alpha'}, {name: 'George', team: 'beta'}, {name: 'Steve', team: 'gamma'}, {name: 'Paula', team: 'beta'}, {name: 'Scruath', team: 'gamma'}]
期望結果:
- team alpha - Gene - team beta - George - Paula - team gamma - Steve - Scruath
答案:
JavaScript:
$scope.players = [ {name: 'Gene', team: 'alpha'}, {name: 'George', team: 'beta'}, {name: 'Steve', team: 'gamma'}, {name: 'Paula', team: 'beta'}, {name: 'Scruath', team: 'gamma'} ];
HTML:
<ul ng-repeat="(key, value) in players | groupBy: 'team'"> <li>Group name: {{ key }}</li> <ul> <li ng-repeat="player in value"> Player: {{ player.name }} </li> </ul> </ul>
>
註:- Group name: alpha - Player: Gene - Group name: beta - Player: George - Player: Paula - Group name: gamma - Player: Steve - Player: Scruath
請記住在模組的依賴項中包含angular.filter。
以上是如何使用 Angular 過濾器按球隊對球員進行分組和顯示?的詳細內容。更多資訊請關注PHP中文網其他相關文章!