0

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

2 Answers 2

1

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"];
}
Sign up to request clarification or add additional context in comments.

Comments

0
var orderBy = $filter('orderBy');
array = orderBy(array, function(row){return row[predicate]}, reverse);
  • array: is the array you need to sort
  • predicate: is the property. It can have spaces, special characters
  • reverse: whether you want to sort in reverse order or not

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.