如何使用功能命令 ng-repeat 使用按钮更改 angularjs 中的顺序参数
How to order ng-repeat with a function a use button to change the order parameter in angularjs
我在尝试使用 angularjs table 时遇到一个小错误。
tr
th
a(href='', ng-click="predicate = 'id'; reverse=!reverse") Id
th
a(href='', ng-click="predicate = 'number'; reverse=!reverse")
th
a(href='', ng-click="predicate ='urlGlobalCount'; reverse=!reverse") Total
tr(ng-repeat='url in URLList | orderBy:predicate:reverse' ng-init="addOne(url)")
td {{$index + 1}}
td {{url.number}}
td {{urlGlobalCount(url)}}
函数 urlGlobalCount return 一个数字
$scope.urlGlobalCount = function(item) {
return *somenumber*
};
当我尝试通过 urlGlobalCount 订购时,它不起作用。
如果我将代码更改为:
tr(ng-repeat='url in URLList | orderBy:urlGlobalCount :reverse' ng-init="addOne(url)")
通过 urlGlobalCount 函数排序工作正常。
有什么办法解决这个问题吗?
在您的点击中指定实际函数,而不是函数的字符串。变化:
ng-click="predicate ='urlGlobalCount';
至:
ng-click="predicate =urlGlobalCount;
这个怎么样?
$scope.predicateFn = function(item) {
return $scope.predicate === 'urlGlobalCount' ?
$scope.urlGlobalCount(item) : item[$scope.predicate];
};
ng-repeat='url in URLList | orderBy:predicateFn:reverse'
我在尝试使用 angularjs table 时遇到一个小错误。
tr
th
a(href='', ng-click="predicate = 'id'; reverse=!reverse") Id
th
a(href='', ng-click="predicate = 'number'; reverse=!reverse")
th
a(href='', ng-click="predicate ='urlGlobalCount'; reverse=!reverse") Total
tr(ng-repeat='url in URLList | orderBy:predicate:reverse' ng-init="addOne(url)")
td {{$index + 1}}
td {{url.number}}
td {{urlGlobalCount(url)}}
函数 urlGlobalCount return 一个数字
$scope.urlGlobalCount = function(item) {
return *somenumber*
};
当我尝试通过 urlGlobalCount 订购时,它不起作用。
如果我将代码更改为:
tr(ng-repeat='url in URLList | orderBy:urlGlobalCount :reverse' ng-init="addOne(url)")
通过 urlGlobalCount 函数排序工作正常。
有什么办法解决这个问题吗?
在您的点击中指定实际函数,而不是函数的字符串。变化:
ng-click="predicate ='urlGlobalCount';
至:
ng-click="predicate =urlGlobalCount;
这个怎么样?
$scope.predicateFn = function(item) {
return $scope.predicate === 'urlGlobalCount' ?
$scope.urlGlobalCount(item) : item[$scope.predicate];
};
ng-repeat='url in URLList | orderBy:predicateFn:reverse'