嵌套数组中angular-filter模块的groupBy过滤器出现问题。 “位置”未定义,“颜色”未输出任何内容。拿这个例子并从其他人的代码中修改它。
提前致谢!
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<script>
angular.module('app',['angular.filter']).controller('MainController', function($scope) {
$scope.players = [
{name: 'Gene', team: 'alpha'[
{location: 'texas',
color: 'blue'}]},
{name: 'George', team: 'beta'[
{location: 'texas',
color: 'red'}]},
{name: 'Steve', team: 'gamma'[
{location: 'kansas',
color: 'purple'}]},
{name: 'Paula', team: 'beta'[
{location: 'kansas',
color: 'green'}]},
{name: 'Scruath', team: 'gamma'[
{location: 'kansas',
color: 'orange'}]}
];
});
</script>
<title>JS Bin</title>
</head>
<body>
<div ng-controller="MainController">
<ul ng-repeat="(key, value) in players | groupBy: 'team.location'">
Group name: {{ key }}
<li ng-repeat="player in value">
player: {{ player.name }} <br/>
favorite color: {{ player.color }}
</li>
</ul>
</div>
</body>
</html>
答案 0 :(得分:4)
您的数据结构与您的代码逻辑不对应。这很好用:
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<script>
angular.module('app',['angular.filter']).controller('MainController', function($scope) {
$scope.players = [
{name: 'Gene',
team: {location: 'texas',
color: 'blue'}
},
{name: 'George', team: {location: 'texas',
color: 'red'}},
{name: 'Steve', team:
{location: 'kansas',
color: 'purple'}},
{name: 'Paula', team:
{location: 'kansas',
color: 'green'}},
{name: 'Scruath', team:
{location: 'kansas',
color: 'orange'}}
];
});
</script>
<title>JS Bin</title>
</head>
<body>
<div ng-controller="MainController">
<ul ng-repeat="(key, value) in players | groupBy: 'team.location'">
Group name: {{ key }}
<li ng-repeat="player in value">
player: {{ player.name }} <br/>
favorite color: {{ player.color }}
</li>
</ul>
</div>
</body>
</html>
&#13;