Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Through my AngularJS application I make calls to an API that return JSON in the following format:

[Object, Object]
0: Object
$$hashKey: "01"
Contact Name: "Michae"
Phone Number: "2000000000000"

As it is shown keys in JSON have spaces, so I used the following code in my app template to display the json content:

<select ng-model ="sortorder">
  <option selected value="{{contacts['Contact Name']}}">Name</option>
  <option selected value="{{contacts['Phone Number']}}">Contact Name</option>
</select>
<br><br><br>    
<div ng-repeat="contacts in mContactsList | orderBy:sortorder">
  <p>Contact Name: {{contacts['Contact Name']}}</p><br>
  <p>Phone: {{contacts['Phone Number']}}</p><br>
</div>

In controller, I tried using:

$scope.sortorder = 'Contact Name'; and $scope.sortorder = '{{contacts[\'Contact Name\']}}';

but none worked...

The problem I am having now is that sorting is not working at all, so can someone please help me by pointing out what exactly I am doing wrong / missing? Any example is highly appreciated

Thanks

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

If you're going to define a custom orderBy -- remember that it takes a function with your current repeated contact as the parameter. Try:

$scope.sortorder = function (contact) {
    return contact["Contact Name"];
}
share|improve this answer
    
Thanks a lot for your help –  MChan Mar 10 at 15:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.