假设我有以下数组users
:
$scope.users = [
{
"name": "foo",
"status": "pending"
},
{
"name": "bar",
"status": "pending"
},
{
"name": "baz",
"status": "active"
},
{
"name": "qux",
"status": "confirmed"
}
]
我正在尝试根据两个status
属性生成分组列表。
输出应该是两个列表
pending, confirmed
-> foo, bar, qux
active
-> baz
我目前正在做的是
ng-repeat="(key, value) in users | groupBy: 'status'"
但这当然失败了,因为输出将是三组(已确认,有效和待定),而不是上面所需的两组。
是否可以使用此ng-repeat
合并这两种状态?
答案 0 :(得分:0)
您可以在控制器中执行此分离,并且您的视图会消耗结果。因此,您将拥有两个ng-repeat
,一个用于状态为待定或已确认的用户,另一个用于具有有效状态的用户。
// I don't have the full code. So I defined a scope object here
// You don't have to define a scope like this, since you inject it
// in you controller.
var $scope = {};
$scope.pendingConfirmed = [];
$scope.active = [];
var users = [
{
"name": "foo",
"status": "pending"
},
{
"name": "bar",
"status": "pending"
},
{
"name": "baz",
"status": "active"
},
{
"name": "qux",
"status": "confirmed"
}
];
for(var i=0; i<users.length; i++){
var user = users[i];
if( user.status === "pending"
|| user.status === "confirmed" ){
$scope.pendingConfirmed.push(user);
} else {
if( user.status === "active" ){
$scope.active.push(user);
}
}
}
console.log('-- Pending or Confirmed --');
console.log($scope.pendingConfirmed);
console.log('-- Active --');
console.log($scope.active);
&#13;
答案 1 :(得分:0)
你可以像这样修复
ng-repeat="(key, value) in users | groupBy: 'status'" | toArray:true
&#13;