5

I have a dynamic data model coming in via a websocket, which looks like this:

var results = [
    [
        {name:'A'},
        {price: 0.00}
    ],
    [
        {name:'C'},
        {price: 0.00}
    ],
]

I'm using my ng-repeat as follows:

ng-repeat="result in results"

Whenever I need to access one of the arrays within the result array I do:

result[0].name

The issue I'm having is that the orderBy filter on ngRepeat doesn't seem to allow me to do this:

ng-repeat="result in results | orderBy: result[0].name

Perhaps it's a basic misunderstanding of how Angular works, but I fail to understand why this wouldn't work. Is it incorrect syntax, or is it due to my data model being dynamic? Should I be setting up a $scope.$apply somewhere?

I've tried with quotes, and I've tried setting up a predicate in the function that parses the data initially, setting predicate to each instance of the result.name as it comes through, but this also doesn't work.

Any help is very much appreciated.

3
  • read the orderBy docs, they explain what you are asking
    – charlietfl
    Jan 21, 2015 at 20:42
  • @tymeJV: The problem is that the given array contains arrays, not single objects. Jan 21, 2015 at 20:57
  • @HimmetAvsar -- Ahh, didn't see that on my first glance.
    – tymeJV
    Jan 21, 2015 at 21:03

1 Answer 1

10

This question is really interesting. Since orderBy will be using the current object, you have to assign the order string relatively.

This will do the trick:

ng-repeat="result in results | orderBy: 'this[0].name'
0

Your Answer

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

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