繁体   English   中英

Orderby加权平均角度

[英]Orderby weighted average in angular

我正在尝试按两列的加权平均值对表进行排序。 列的权重存储在控制器的作用域中。 当我尝试在orderB中引用这些权重时,表达式排序不正确。

 <tr ng-repeat = "x in fruitdata | orderBy:'cost.apples*apples + cost.oranges * oranges'">

我想要的小提琴: https : //jsfiddle.net/t3op50p9/2/

如果我对权重进行硬编码,那么一切都会正常运行

 <tr ng-repeat = "x in fruitdata | orderBy:'1.89*apples + 1.49 * oranges'">

用硬编码的砝码提琴(不是我想要的): https : //jsfiddle.net/t3op50p9/3/

您可以在范围内定义一个函数,用于计算订购权重

$scope.orderFn = function (fruit) {
    return fruit.apples * $scope.cost.apples + fruit.oranges * $scope.cost.oranges;
}

ng-repeat="x in fruitdata | orderBy: orderFn:true" (注意:最后的true颠倒排序功能所产生的顺序,使排序权重最大。)

https://jsfiddle.net/TheSharpieOne/t3op50p9/6/

您应该使用cost.applescost.oranges不应位于'单引号内,因为它们不是ng-repeat重复数组的一部分,因此它们将作为字符串连接绑定。

标记

<tr ng-repeat="x in fruitdata | orderBy:cost.apples+'*apples + '+ cost.oranges+' * oranges'">
    <td> {{x.oranges}} </td>
    <td> {{x.apples}} </td>
    <td> {{x.apples * cost.apples + x.oranges * cost.oranges}} </td>
</tr>

工作小提琴

暂无
暂无

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

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