【问题标题】:orderBy does nothing when fed by a function result当由函数结果提供时 orderBy 什么都不做
【发布时间】:2013-09-24 07:24:34
【问题描述】:

我使用ng-repeat 来显示任务列表:

<div class="task_container" ng-repeat="task in tasks | orderBy:weigth(task)">
    <span >Task:<span><br><span>{{task.label}}</span>
</div>

每个任务都有一些属性,我定义了一个权重函数来总结这些属性:

function weight(task)
{
    var weight_value = task.priority - task.difficulty + task.interest;
    return weight_value;
}

但是,在显示页面时,任务根本没有排序。

有没有更好/正确的方法来实现这一目标?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    就这么做吧:

    <div class="task_container" ng-repeat="task in tasks | orderBy:weigth">
        <span >Task:<span><br><span>{{task.label}}</span>
    </div>
    

    这里有一个 jsFiddle:http://jsfiddle.net/pkozlowski_opensource/zjvsu/1/

    另一个完整的帖子在这里:Custom sort function in ng-repeat

    并将您的权重函数附加到 $scope

    【讨论】:

    • 我犯的另一个错误是我没有在 $scope 中定义我的权重函数。
    • 哦,我认为这只是例子,我更新了答案,你接受了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多