陣列基於陣列看起來像這樣的:組對象的AngularJS
var members = [
{name: "john", team: 1},
{name: "kevin", team: 1},
{name: "rob", team: 2},
{name: "matt", team: 2},
{name: "clint", team: 3},
{name: "will", team: 3}
];
我需要爲每個團隊的無序列表。
可以用ngRepeat直接完成,也可以使用過濾器嗎?
或者更容易將數組重組爲一組成員隊列的數組?
var teams = [
{id: 1, members: ["john", "kevin"]},
{id: 2, members: ["rob", "matt"]},
{id: 3, members: ["clint", "will"]}
]
嵌套的ngRepeat很容易實現,但是如何以簡單/聰明的方式從第一個數組到第一個數組?
注意:數組不是來自數據庫,而是來自html表。所以這只是一個平坦的成員名單。
function MyController() {
this.members = [
{name: "john", team: 1},
{name: "kevin", team: 1},
{name: "rob", team: 2},
{name: "matt", team: 2},
{name: "clint", team: 3},
{name: "will", team: 3}
];
}
angular.module('app', []);
angular.module('app')
.controller('MyController', MyController);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="MyController as ctrl">
<ul>
<li ng-repeat="member in ctrl.members">{{ member.name }} - {{ member.team }}</li>
</ul>
</div>
</div>
這個鏈接將幫助您too..http://stackoverflow.com/questions/14800862/how-can-i-group-data-with-an-angular-過濾器 –