我正在开发 Angular 应用程序,我有带有键和值的数据对象,见下文
var data=[{key:"home",value:"hk1"},{key:"home",value:"hk2"},{key:"home",value:"hk3"},{key:"home",value:"hk4"},
{key:"product",value:"pk1"},{key:"product",value:"pk2"},{key:"product",value:"pk3"},{key:"product",value:"pk4"},
{key:"service",value:"sk1"},{key:"service",value:"sk2"},{key:"service",value:"sk3"},{key:"service",value:"sk4"},
];
我的页面是
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>Angular Boiler Plate</title>
<script src="/angular.js"></script>
<script>
var app=angular.module('myApp',[]);
function mainCtrl($scope){
$scope.Maindata=[{key:"home",value:"hk1"},{key:"home",value:"hk2"},{key:"home",value:"hk3"},{key:"home",value:"hk4"},
{key:"product",value:"pk1"},{key:"product",value:"hk2"},{key:"product",value:"hk3"},{key:"product",value:"hk4"},
{key:"service",value:"sk1"},{key:"service",value:"hk2"},{key:"service",value:"hk3"},{key:"service",value:"hk4"},
];
}
</script>
</head>
<body ng-controller="mainCtrl">
<h2 ng-repeat="data in Maindata" class="">
{{data.key}}
<div ng-repeat="">{{data.value}}</div>
</h2>
</body>
</html>
我想将具有相同键的对象分成一个单独的对象,例如
var filtereddata=[{"home":"[{value:"hk1"},{value:"hk2"},{value:"hk3"},{value:"hk4"}]"},
{"product":"[{value:"pk1"},{value:"pk2"},{value:"pk3"},{value:"pk4"}]"},
{"service":"[{value:"sk1"},{value:"sk2"},{value:"sk3"},{value:"sk4"}]"},
]
这样我就可以在页面中一次使用“home”、“service”和“product”作为标题,并使用 ng-repeat 将每个标题中存在的那些值迭代到列表中。
在这里,我将使用两个 ng-repeat
用于标题,即主页、服务和产品。另一个用于迭代每个标头中的数据。
请帮助我使用过滤器实现这一目标。提前感谢您的帮助。