8

This is my code

<a href="" ng-click="predicate = 'productname'; reverse=false">Productname</a>
<a href="" ng-click="predicate = 'productprice'; reverse=false">Productprice</a>

Iteration

 <div ng-repeat="sale in sales | orderBy:customSort">

Customsort function

$scope.customSort = function(sale) {


 };

Currently in the customSort function I get all the sale data but I also want to pass the predicate value to the function so that it can sort accordingly(sort by name if name is clicked, sort by price if price predicate is clicked.)

How can I pass the predicate value to the customSort function?Can any one please help me with this? Thanks.

4
  • 2
    use a colon in markup to delimit arguments see filter docs Commented May 18, 2014 at 23:59
  • @charlietfl this should be an answer Commented May 19, 2014 at 0:06
  • @charlietfl orderBy appears to just take an expression Commented May 19, 2014 at 0:13
  • @MatthewMcveigh can also use custom filter function without orderBy , not 100% clear what intent is here because mixing several issues Commented May 19, 2014 at 0:16

1 Answer 1

18

You could call your custom sort with the predicate and return a closure being your original function, now your function has access to predicate:

<div ng-repeat="sale in sales | orderBy:customSort(predicate)">

$scope.customSort = function(predicate) {
    return function(sale) {


    };
};
Sign up to request clarification or add additional context in comments.

2 Comments

This solved my problem for passing $viewValue into the return function, however I wonder how performant something like this is. I note that on every keystroke, the custom sort 'factory' let's just call it, gets called. gist.github.com/jusopi/283887919b87cba5edb6
@jusopi if it's only called on every keystroke i think it's unlikely to have any noticeable impact. if it was called on each iteration of the sort then there could be troubles, but that shouldn't happen

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.