ani*_*CSE 10 unique filter unique-key angularjs ng-repeat
我正在为我的页面使用angularjs.我想过滤JSON对象中的值,因此不存在冗余.但我没有找到任何方法来获得角度ng-repeat的唯一值.反正有吗?
好的,这是关于这个问题的一些描述.我有这种格式的JSON.这是我从服务获得的JSON.所以我们不能指望重复数据是如何发生的.
result = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
}
]
Run Code Online (Sandbox Code Playgroud)
我希望输出JSON没有重复的元素,所以我的输出将是这样的
result = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
}
]
Run Code Online (Sandbox Code Playgroud)
cal*_*tie 17
您可以使用已unique
定义过滤器的Angular UI .
来源可以在这里找到.
基本上,您可以按如下方式使用过滤器:
<div ng-repeat="item in result | unique:'_id'">
//Body here
</div>
Run Code Online (Sandbox Code Playgroud)
您可以在angular.filter模块中使用"unique"(别名:uniq)过滤器(https://github.com/a8m/angular-filter)
用法:colection | uniq: 'property'
您可以通过嵌套属性过滤到:colection | uniq: 'property.nested_property'
所以你可以做那样的事......
function MainController ($scope) {
$scope.orders = [
{ id:1, customer: { name: 'foo', id: 10 } },
{ id:2, customer: { name: 'bar', id: 20 } },
{ id:3, customer: { name: 'foo', id: 10 } },
{ id:4, customer: { name: 'bar', id: 20 } },
{ id:5, customer: { name: 'baz', id: 30 } },
];
}
Run Code Online (Sandbox Code Playgroud)
HTML:我们按客户ID过滤,即删除重复的客户
<th>All customers list: </th>
<tr ng-repeat="order in orders | unique: 'customer.id'" >
<td> {{ order.customer.name }} , {{ order.customer.id }} </td>
</tr>
Run Code Online (Sandbox Code Playgroud)
结果:
所有客户列表:
foo 10
bar 20
baz 30
归档时间: |
|
查看次数: |
41087 次 |
最近记录: |