sabbir sabbir - 25 days ago 16
AngularJS Question

Filter ng-repeat using object property

I have an array as like as given below:

$scope.athletes= [
{
name: 'name 01',
measurements: [
{
type: 'date'
}, {
type: 'info',
value: 23,
rank: 1
}]
},
{
name: 'name 02',
measurements: [
{
type: 'date'
}, {
type: 'info',
value: 13,
rank: 3
}]
},
{
name: 'name 03',
measurements: [
{
type: 'date'
}, {
type: 'info',
value: 17,
rank: 2
}]
}];


I want to display results based on
rank
filter. I already have tried something, but that doesn't work. My code is given below:

<tr ng-repeat="athlete in filteredEntries = (athletes | filter: measurements[1].rank )">

<td> {{ athlete.name }} </td>

<td > {{ athlete.measurements[1].value }} </td>

</tr>


And shown ouput :

Name --- value
===========================
name 01 --- 23
---------------------------
name 02 --- 13
---------------------------
name 03 --- 17


But I want the following output:

Name --- value
===========================
name 01 --- 23
---------------------------
name 03 --- 17
---------------------------
name 02 --- 13

Answer

You should be using orderBy, check below jsfiddle:

https://jsfiddle.net/twizzlers/7peLbu2m/1/

<div ng-repeat="athlete in athletes | orderBy: 'measurements[1].rank' ">
   <div> {{ athlete.name }} </div>
   <div > {{ athlete.measurements[1].value }} </div>
</div>

Hope it helps =)

Comments