I'd like to have an ng-repeat that's sorted by a value in some external lookup table.
For example, suppose I have a list of items
with an itemType
for each item. I have a separate itemTypePriorities
lookup table in the scope, such that itemTypePriorities[someItemType]
gives the priority for a given item type. Now, I'd like to display items in order of their priorities. I am trying the following:
<div ng-repeat="item in items | orderBy:'itemTypePriorities[item.itemType]'>
But it doesn't seem to work - my guess is that it's interpreting the orderBy expression in the context of item
, i.e. it's trying to order by the nonsensical expression item.itemTypePriorities[item.itemType]
.
Is there a way to rewrite the ng-repeat to make it wok correctly, without having to modify the items
or itemTypePriorities
arrays in any way?
You can use of course the orderBy filter but I think you will need to create a custom function to orderit.
For example:
<div ng-repeat="item in items | orderBy:myFunction>
Where myFunction is declared somewhere in your controller as:
$scope.myFunction = function(el){
//el is the current element under evaluation
}
And should return a value that will be compared with <, === and > with the other. In your case the function can be:
$scope.myFunction = function(el){
return itemTypePriorities.indexOf(el);
}
And that should work