Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass extra parameters to custom sort function in AngularJS

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.

like image 942
user727728 Avatar asked May 18 '14 23:05

user727728


1 Answers

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) {


    };
};
like image 115
Matthew Mcveigh Avatar answered Oct 20 '22 09:10

Matthew Mcveigh