Lionel B Lionel B - 1 month ago 5
Javascript Question

Angular Js - Get a variable in Controller

I have a ladder , my users are order by score and i use a variable in a ng-repeat to know the position of my user in ladder

<tr ng-repeat="user in users| orderBy:'-score'" ng-init="pos=0">
<td>{{pos + $index + 1}}</td>
<td>{{user.email}}</td>
<td>{{user.score}}</td>
<tr>


I want to keep this data in the controller , each user must have a rank

{{pos + $index + 1}}


Example :

{user :
"score":12,
"email":test,
"rank": (result of var {{pos + $index + 1}})
}


I want to know how to get the result of his value in my controller

Thanks

Answer

In html

{{user.rank = pos + $index + 1}}

above expression assigns result of pos + $index + 1 to user.rank within user object. So, every user object have its own rank kept track seperatly.

In controller you can access each rank using

$scope.users[0].rank
$scope.users[1].rank
Comments