繁体   English   中英

将额外参数传递给AngularJS中的自定义排序函数

[英]Pass extra parameters to custom sort function in AngularJS

这是我的代码

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

迭代

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

海关功能

$scope.customSort = function(sale) {


 };

目前在customSort函数中,我得到了所有的销售数据,但我也希望将谓词值传递给函数,以便它可以进行相应的排序(如果点击名称,则按名称排序,如果单击价格谓词,则按价格排序。)

如何将谓词值传递给customSort函数?任何人都可以帮我这个吗? 谢谢。

您可以使用谓词调用自定义排序并返回一个闭包作为原始函数,现在您的函数可以访问谓词:

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

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


    };
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM