[英]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
顛倒排序功能所產生的順序,使排序權重最大。)
您應該使用cost.apples
和cost.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.