Home Sort by value in ng-repeat (key, value) pair from object
Reply: 0

Sort by value in ng-repeat (key, value) pair from object

user1164
1#
user1164 Published in May 25, 2018, 6:51 pm

I'm new to AngularJS. Using API I am getting key - value pair with sports

$scope.sports = { 1: "Soccer", 2: "Tennis", 3: "Basketball" ... };

Is this possible to sort items by sport name:

<ul>
  <li ng-repeat="(id, value) in sports | orderBy:'value'">
      <a ng-href='/page/{{id}}'>{{value}}</a>
  </li>
 </ul>

this doesn't work at all

https://jsfiddle.net/ak657soa/1/

 now is
--------------------
    Soccer // id: 1
    Tennis // id: 2
    Basketball // id: 3
    MMA // id: 4
    Street dance // id: 5
    Aerobics // id: 6
    Aerial hoop // id: 7

should be
    --------------------
    Aerial hoop // id: 7
    Aerobics // id: 6
    Basketball // id: 3
    MMA // id: 4
    Soccer // id: 1
    Street dance // id: 5
    Tennis // id: 2

I tried to sort them inside controller with underscore.js (I used it in different view) but then I lose information about ids. Like here: https://jsfiddle.net/vmbme3yw/1/

How can I deal with orderBy value in AngularJs where are (key, value) pairs?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303102 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO