Using orderBy for ng-repeat with a multidimensional array

I have a dynamic data model coming in through a websocket that looks like this:

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

I use my ng-repeat as follows:

 ng-repeat="result in results" 

Whenever I need to access one of the arrays in the result array, I:

 result[0].name 

The problem I am facing is that the orderBy filter on ngRepeat does not allow me to do this:

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

This may be a basic misunderstanding of how Angular works, but I don't understand why this will not work. Is this the wrong syntax, or is it related to a dynamic data model? Should I customize the $ scope.

I tried with quotes and I tried to set up a predicate in a function that first parses the data, setting a predicate for each instance of result.name as it appears, but this also does not work.

Any help is greatly appreciated.

+5
source share
1 answer

This question is really interesting. Since orderBy will use the current object, you need to assign an order string relatively.

This will do the trick:

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

Source: https://habr.com/ru/post/1211652/


All Articles