1

I'm entirely sure I'm going about this the wrong way but seeing how new I am with angularjs I can't figure out how to accomplish what I want. I have a set of users that I am loping through using ng-repeat, but I can't figure out how to filter by the scope variable userId. The User id: <name> is being printed out just fine, so there is no issues with the scope. How can I have the ng-repeat loop sort by this userId? If I'm going about it the wrong what, can you please link me documentation that will help me go about it the right way.

HTML:

User id: {{userId}}
<br>
User Name:
<ul>
    <li ng-repeat="user in users | filter:{{userId}}">
        {{user.name}}
    </li>
</ul>

Controller:

function PhoneDetailController($scope, $routeParams, Users){
     $scope.userId = $routeParams.userId;
}
Ducksauce88
  • 640
  • 3
  • 12
  • 26

2 Answers2

2

You can simply use a $scope variable userId in ng-repeat like so

<ul>
    <li ng-repeat="user in users | filter:userId">
        {{user.name}}
    </li>
</ul>

Basically just no need of angular expressions {{}} since it is already inside an expression.

Couple of links to help you out if you need.

Here is a blog explaining Two Thumb Rules when deciding whether or not to use curlies.

When exactly to use double curly braces, single curly braces and no braces

Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44
0

If the structure of users is like this:

[
    {id:1,name:'xxx'},
    {id:2,name:'yyy'}
]

You can use the 'orderBy' filter to sort it and use filter filter to filter it:

<ul>
    <li ng-repeat="user in users | filter:{id:userId} | orderBy: 'id' ">
        {{user.name}}
    </li>
</ul>

id is a property of user in ng-repeat.

And here are the documentations: filter and orderBy

awmleer
  • 1,658
  • 3
  • 13
  • 28