问题描述:

I am having trouble with the proper syntax for creating a custom orderBy filter. I have no problems if require one parameter ( the item being iterator over in ng-repeat ). How do you pass a second parameter. I have tried:

 <div ng-repeat="item in items | orderBy:myCustomSort:mySecondParameter"></div>

However if I put a break in the myCustomSort function, mySecondParameter is undefined. Additionally can you have orderBy reverse the order as well and if so where do you stick the "reverse" parameter.

Thanks.

网友答案:

Please have a look at: http://docs.angularjs.org/api/ng.filter:orderBy. The parameter for the orderBy filter could be a string, function or an array. So you may write:

<div ng-repeat="item in items | orderBy:[myCustomSort,mySecondParameter]"></div>

to sort your data by two properties.

As an example see this fiddle: http://jsfiddle.net/kFHXL/ if you click on the "Age" you can see that the data are sorted by name and after that by age.

网友答案:

I could not find a solution to pass an additional parameter into the sort function. Instead I just created an appendItemFilter which takes the argument I wanted to reference in the customSort and appends it to each item. Then I can just reference it directly in the item. so:

<div ng-repeat="item in items | appendItem:mySecondParameter | orderBy:myCustomSort"></div>
相关阅读:
Top